2
我已閱讀幾乎每篇關於使用publishProgress在進度對話框上設置新消息的文章,而且我根本無法使其發揮作用。無法使用publishProgress方法更新AsyncTask中的進度對話框消息
代碼:
public class LoginToApi extends AsyncTask<String, TaskProgress, String> {
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setCancelable(false);
pDialog.setMessage("Logging you in...");
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
HashMap<String, String> loginMap = new HashMap<String, String>();
loginMap.put("username", un.getText().toString());
loginMap.put("password", pw.getText().toString());
loginMap.put("session_id", "zOhAVPADzsGk0MZiMdXUzYLErP44pVZGWdHU7yD_Paek169umn");
String loginXmlString = xmlCon.createXML("login", loginMap);
String playlistDetails = xmlParse.makeHttprequest(url_login, loginXmlString);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/adgen");
File loginFile = new File(path, "login.xml");
publishProgress(new TaskProgress(30, "A new update"));
try {
if(saveStringToFile(path, loginFile, playlistDetails)) {
Log.i("main", "login xml Saved");
// Read play.xml to array
//xmlParse.parse(file);
// Get playlist using playlist ID
HashMap<String, String> playlistMap = new HashMap<String, String>();
playlistMap.put("id", "158");
String playlistDetailsString = xmlCon.createXML("playlist", playlistMap);
String playlist = xmlParse.makeHttprequest(url_get_playlist, playlistDetailsString);
File playlistFile = new File(path, "playlist.xml");
try {
if(saveStringToFile(path, playlistFile, playlist)) {
Log.i("main", "playlist xml Saved");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//Intent i = new Intent(getApplicationContext(), WebViewActivity.class);
//startActivity(i);
//finish();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void onProgressUpdate(TaskProgress progress) {
pDialog.setProgress(progress.percentage);
pDialog.setMessage(progress.message);
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
private static class TaskProgress {
final int percentage;
final String message;
TaskProgress(int percentage, String message) {
this.percentage = percentage;
this.message = message;
}
}
我得到的零個錯誤或警告,我的代碼運行正常,與正在救了我的文件,並最終異步任務完成後,被刪除的進度對話框。但是當我使用publishProgress時,對話框消息不會更新。
我使用this post作爲指導,但正如您所看到的,即使在給出解決方案之後,OP看起來也很麻煩。
我在做什麼錯?
Thanks1
什麼是 「...」 是什麼意思?對不起,正如我所說的,我是Android/Java的新手,在網上找到有用的信息(這很容易理解和深入),它的命中率和錯失率 –
閱讀此:(http://stackoverflow.com/a/6343250/833647) –
此外,爲避免由於方法簽名不正確而導致的這些問題,最好在方法聲明之前放置'@Override'註釋。 – TactMayers