Вопрос

I tried to stop AsyncTask on onPause() method. But its not stop at all. When my App navigate to other Activity AsyncTask still on work.

this is my code... Motion1, Motion2 and Motion3 are ImageViews.

class parseComments extends AsyncTask<String, Integer,String> 
{

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        cancel(true);
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                centerImage.setVisibility(View.INVISIBLE);
                Motion1.setVisibility(View.VISIBLE);
                guiLayout1.addView(authHint1);
                 DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
                    int width = metrics.widthPixels;
                    int height = metrics.heightPixels;
                TextView textView = new TextView(guiLayout1.getContext());

                mProgressBar = new ProgressBar(guiLayout1.getContext(), null, android.R.attr.progressBarStyleHorizontal);
                Resources res = getResources();
                mProgressBar.setProgressDrawable(res.getDrawable( R.drawable.my_progress));
                //mProgressBar.setProgress(50);         
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams((int)(width/(float)1.5), 22);
                params.setMargins(0, (int) (height /(float) 4.7) , 0, 0);
                params.gravity= Gravity.CENTER_HORIZONTAL;
                mProgressBar.setLayoutParams(params);

                guiLayout1.addView(textView); 
                guiLayout1.addView(mProgressBar);
                progressStatus = 60;
                mProgressBar.setProgress(progressStatus);

                Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(200);
                progressStatus += 10;
                mProgressBar.setProgress(progressStatus);                               
                //authHint1.nextStep();

            }
        });
        try {
            Thread.sleep(timeInterval * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Motion1.setVisibility(View.INVISIBLE);
                Motion2.setVisibility(View.VISIBLE);
                Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(200);
                progressStatus += 10;
                mProgressBar.setProgress(progressStatus);
                authHint1.nextStep();
            }
        });

        try {
            Thread.sleep(timeInterval * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Motion2.setVisibility(View.INVISIBLE);
                Motion3.setVisibility(View.VISIBLE);
                Vibrator v= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(200);
                progressStatus += 10;
                mProgressBar.setProgress(progressStatus);
                authHint1.nextStep();
            }
        });

        try {
            Thread.sleep(timeInterval * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
    protected void onPostExecute() {
    }

    @Override
    protected void onPreExecute() {


    }
}

And onPause() I called new parseComments().cancel(true);

But its not working. How can i stop it?

Это было полезно?

Решение

The best answer I can get to you is: please, read documentation: http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

And some questions for you:
1) Why do you expect that calling cancel(true) in doInBackground method will exit this method? Read documentation to make this clear.
2) For what reason you use AsyncTask, if ALL the code is running in the UI thread? Read documentation to know how and where to use AsyncTasks. http://developer.android.com/reference/android/os/AsyncTask.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top