如何同时doInBackground在AsyncTask的运行对话框辞退? [英] How to dismiss dialog while doInBackground is running in asynctask?

查看:109
本文介绍了如何同时doInBackground在AsyncTask的运行对话框辞退?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了doInBackGround的AsyncTask方法运行时从mainthread取消对话框。当我下载一张照片,一个进度对话框弹出,当它完成下载我dismis()在onPostExecute对话框。如果连接速度慢,该对话框了一段时间,直到有一个超时错误或完成下载我不能取消它。如何使用后退按钮所以主线程可以访问。这里是我的code样子:

 私有类DownloadImageTask扩展的AsyncTask<弦乐,太虚,位图> {
    在preExecute保护无效(){
        //这块code似乎不工作
        progressDialog = ProgressDialog.show(上下文,,
                图像加载,真正的);
    }    保护位图doInBackground(字符串的URL ...){
        尝试{
            在的InputStream =新的java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.de codeStream(中);
        }赶上(例外五){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回mIcon11;
    }    保护无效onPostExecute(位图结果){
        //bmImage.setImageBitmap(result);
        progressDialog.dismiss();
        的someMethod(结果);    }
}


解决方案

使用一个撤销的进度对话框,传递一个取消监听器的进度对话框和方法中取消任务,例如:

 上preExecute保护无效(){
    progressDialog = ProgressDialog.show(活动,搜索文件,扫描...,真实的,真实的,
            新DialogInterface.OnCancelListener(){
                @覆盖
                公共无效onCancel(DialogInterface对话){
                    //当取消对话框,需要明确取消任务,否则它在后台继续运行
                    取消(真);
                }
            }
    );    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}

I am trying to cancel a dialog from the mainthread while the 'doInBackGround' method of AsyncTask is running. While I am downloading a photo, a progress dialog pops up and when it is finished downloading I dismis() the dialog in onPostExecute. If the connection is slow, the dialog is up for a while and I cannot cancel it until there is a timeout error or it finishes downloading. How do I use the back-button so the main thread can access. Here is what my code looks like:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {


    protected void onPreExecute() {
        //this piece code doesn't seem to work
        progressDialog = ProgressDialog.show(context, "",
                "Image loading", true);
    }

    protected Bitmap doInBackground(String... urls) {
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        //bmImage.setImageBitmap(result);
        progressDialog.dismiss();
        someMethod(result);

    }
}

解决方案

Use a cancellable progress dialog, pass in a cancel listener to the progress dialog and cancel the task within that method, eg

protected void onPreExecute() {
    progressDialog = ProgressDialog.show(activity, "Searching files", "Scanning...", true, true,
            new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    // When dialog in cancelled, need to explicitly cancel task otherwise it keeps on running
                    cancel(true);
                }
            }
    );

    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}

这篇关于如何同时doInBackground在AsyncTask的运行对话框辞退?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆