Android progressdialog和asynktask冲突

Android progressdialog和asynktask冲突,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我开发了一个简单的asynctask,附带progressdialog,以显示给定任务的进度 它们工作得很好,没有任何问题,同时没有方向变化。当我改变我的设备的方向时(我正在真实的设备上测试),对话框出现在窗口中 我试图在方向改变时手动关闭进度对话框,但似乎没有任何东西影响该对话框 以下是我的任务代码: class DownloadFileAsync extends AsyncTask<String, String, String>{ @Override prote

我开发了一个简单的asynctask,附带progressdialog,以显示给定任务的进度

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

我试图在方向改变时手动关闭进度对话框,但似乎没有任何东西影响该对话框

以下是我的任务代码:

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);
    }

}
最后,调用异步任务

new DownloadFileAsync().execute(fileURL);
你能找到任何导致这种取向改变的错误行为吗

提前谢谢大家,


Vyrphan

在Android清单文件的活动中添加以下属性

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

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


当你改变设备的方向时会产生问题,对吗?我已经尝试了过去的解决方案,万分感谢!配置更改是我所缺少的。现在一切都很好:Ddone!我是stackoverflow的新手,我根本不知道它是怎么工作的
<activity android:name=".name" android:label="@string/app_name" 
    android:configChanges="touchscreen|keyboard|keyboardHidden|navigation|orientation"></activity>