【问题标题】:Android progressdialog and asynktask conflictAndroid progressdialog 和 asynctask 冲突
【发布时间】:2012-03-27 07:31:59
【问题描述】:

我开发了一个简单的异步任务,并附加了进度对话框,以显示给定任务的进度。

它们工作得很好,没有任何问题,而且没有发生方向变化。我改变了设备的方向(我正在真实设备上测试),对话框出现在窗口中。

当方向改变时,我尝试手动关闭该进度对话框,但似乎没有任何影响该对话框。

这是我的异步任务代码:

class DownloadFileAsync extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DOWNLOAD_PROGRESS_DIALOG);
    }

    @Override
    protected String doInBackground(String... params) {
        File file = null;
        try {
            URL url = new URL(fileURL);
            file = new File(fileName);

            if(!file.exists()){
                file.createNewFile();
            }else{
                return "0";
            }

            /* Open a connection to that URL. */
            URLConnection ucon = url.openConnection();

            int lenghtOfFile = ucon.getContentLength();
            /*
             * Define InputStreams to read from the URLConnection.
             */
            InputStream is = ucon.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);

            byte[] buffer = new byte[1024];
            int len1 = 0;
            long total = 0;
            while((len1 = is.read(buffer)) > 0){
                total += len1;
                publishProgress("" + (int) ((total*100)/lenghtOfFile));
                fos.write(buffer, 0, len1);
            }
            fos.close();

            return "1";
        } catch (IOException e) {
            if(file != null && file.exists()){
                file.delete();
            }
            dismissDialog(DOWNLOAD_PROGRESS_DIALOG);

        }
        return "0";
    }
    @Override
    protected void onProgressUpdate(String... values) {
        try{
            downloadProgress.setProgress(Integer.parseInt(values[0]));
        }catch(Exception e){

        }
    }

    @Override
    protected void onPostExecute(String result) {
        downloadResult = result;    
        dismissDialog(DOWNLOAD_PROGRESS_DIALOG);
    }

}

以下是我的 oncreatedialog

@Override
protected Dialog onCreateDialog(int id) {
...
}case(DOWNLOAD_PROGRESS_DIALOG):{
        if(downloadProgress == null){
            downloadProgress = new ProgressDialog(this);
            downloadProgress.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) { 
                    if(downloadResult == "1"){
                        FileStored newFileStored = new FileStored(fileName, new Date());
                        db.insertFile(newFileStored);
                        refreshLastDownload();
                        downloadResult = null;
                    }else{
                        showDialog(DOWNLOAD_PROBLEM);
                    }
                    downloadProgress = null;
                }
            });
            Resources r = getResources();
            downloadProgress.setMessage(r.getString(R.string.downloading));
            downloadProgress.setIndeterminate(false);
            downloadProgress.setMax(100);
            downloadProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            downloadProgress.setCancelable(true);
            downloadProgress.show();
        }
        return downloadProgress;
    }case(DOWNLOAD_PROBLEM):{
        if(this.downloadProblemDialog == null){
            TextView problemView = new TextView(this);
            problemView.setText(R.string.problem_download_text);
            AlertDialog.Builder problemDialog = new AlertDialog.Builder(this);
            problemDialog.setTitle(R.string.problem_download);
            problemDialog.setView(problemView);
            problemDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            this.downloadProblemDialog = problemDialog.create();
        }
        return this.downloadProblemDialog;
    }
...

最后是异步任务的调用

new DownloadFileAsync().execute(fileURL);

你能找到导致这种方向改变奇怪行为的任何错误吗?

提前谢谢你,

维芬

【问题讨论】:

  • 当你改变设备的方向时它会产生问题吗?
  • 我有过去的解决方案尝试,

标签: android android-asynctask progressdialog


【解决方案1】:

在 Android Manifest 文件的 Activity 中添加以下属性。

<activity android:name=".name" android:label="@string/app_name" 
    android:configChanges="touchscreen|keyboard|keyboardHidden|navigation|orientation"></activity>

【讨论】:

  • 万分感谢! configChanges 是我所缺少的。现在一切都很好:D
  • 完成!我真的是 stackoverflow 的新手,我根本不知道它是如何工作的
【解决方案2】:

当设备的方向改变时,android 生命周期从 onCreate 方法重复。 因此,要处理跨方向更改的进度对话框,您有两种选择,一种是将活动的 configchanges 标志设置为方向。其他选项是在配置更改中保留状态,请参阅链接:

How to save state during orientation change in Android if the state is made of my classes?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2014-12-18
    • 1970-01-01
    • 2018-01-28
    • 2014-05-29
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多