我已經開發了一個簡單的asynctask,並附有progressdialog,以顯示給定任務的進度。Android progressdialog和asynktask衝突
他們工作很好,沒有任何問題,而沒有方向變化發生。我改變了設備的方向(我在真實設備上測試),對話框出現在窗口中。
我嘗試了手動解除進度對話框,當方向改變時,但沒有任何東西似乎影響對話框。
這裏是我的AsyncTask代碼:
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);
你能發現什麼不對的線索到這個方向改變怪異的行爲?
謝謝你在前進,
Vyrphan
當您更改的裝置右方向它創造的問題? – Hasmukh 2012-03-27 07:36:45
我過去的解決方案嘗試,, – Hasmukh 2012-03-27 07:40:44