强制停止,杀死或取消AsyncTask

时间:2014-05-13 11:57:32

标签: android android-asynctask ui-thread

我试图在onPause()方法上停止AsyncTask。 但它根本没有停止。当我的应用程序导航到其他活动AsyncTask仍在工作时。

这是我的代码...... Motion1,Motion2和Motion3是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() {


    }
}

onPause()我打来电话     new parseComments()。cancel(true);

但它不起作用。 我怎么能阻止它?

1 个答案:

答案 0 :(得分:0)

我能得到的最佳答案是:请阅读文档: http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

还有一些问题要问你:
1)为什么您希望在cancel(true)方法中调用doInBackground将退出此方法?阅读文档以明确这一点 2)出于什么原因使用AsyncTask,如果 ALL 代码在UI线程中运行? 阅读文档以了解如何以及在何处使用AsyncTasks。 http://developer.android.com/reference/android/os/AsyncTask.html