在AsyncTask中,取消然后将setResult取消为Result_Canceled

时间:2017-12-14 17:46:55

标签: android android-asynctask onactivityresult cancellation

我正在运行AsyncTask来处理多个图像。我只能检测到操作应该在运行时停止/取消,并且必须终止线程并返回到先前的意图。如何停止线程运行并返回上一个活动。到目前为止,代码退出当前循环但继续运行并产生错误和不良结果

            private class processImagesThread extends AsyncTask<String, Integer, Bitmap> {    
                @Override
                protected Bitmap doInBackground(String... params) {
                for (int i = 0; i < ArrayUris.size(); i++) {
                    if(failCondition){
                        Intent resultIntent = new Intent();               
                        setResult(Activity.RESULT_CANCELED);//normally return ok and a URI
                        this.cancel(true);
                        finish();
                        break;
                    }
                }
                //further processing continues after failcondition
                }
                @Override
                protected void onCancelled()
                {
                    super.onCancelled();
                    Intent resultIntent = new Intent();
                    setResult(Activity.RESULT_CANCELED);

                }
            }

            //previous intent
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == RESULT_CANCELED) { //Toast to user}}

1 个答案:

答案 0 :(得分:0)

我重新编译并使用日志而不是调试器运行,并且使用现有代码运行。注意是否有局部故障。谢谢Pavan,即使在打开调试器的情况下,“ return null”也完全杀死了线程,但是那一次我没有去过onpostexecute。