异步任务在运行时触摸屏幕时是否被取消?

时间:2012-11-09 06:36:38

标签: android android-asynctask

异步任务在运行时触摸屏幕时开始取消。我已经使用 android 2.3.6 samsung pop 中测试了我的应用程序。在此手机中异步任务在运行时触摸屏幕时不会被取消。但是当我在索尼你用android 4.0.1 测试它时触摸屏幕异步任务被取消了。我试过用异步状态覆盖触摸事件。但它没有得到修复。我们可以解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我找到了解决方案

我用

  

dialog.setCanceledOnTouchOutside(假);               dialog.setCancelable(假);

public class getcatogry extends AsyncTask<String, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(
            MainActivity.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
    }

    // automatically done on worker thread (separate from UI thread)
    protected Void doInBackground(final String... args) {
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);






        return null;
    }

    // can use UI thread here
    protected void onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();




        }
        // reset the output view by retrieving the new data
        // (note, this is a naive example, in the real world it might make
        // sense
        // to have a cache of the data and just append to what is already
        // there, or such
        // in order to cut down on expensive database operations)
        // new SelectDataTask().execute();
    }
}

这样即使在反压或触摸异步任务

上也不会取消异步任务