我已经阅读了几乎所有关于使用publishProgress在进度对话框上设置新消息的帖子,而我根本无法让它工作。
代码:
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;
}
}
我得到零错误或警告,我的代码运行正常,我的文件被保存,最终在Async任务完成后删除了进度对话框。但是当我使用publishProgress时,对话框消息根本不会更新。
我使用this post作为指导,但正如您所看到的,即使在提供解决方案之后,OP看起来也有问题。
我做错了什么?
Thanks1
答案 0 :(得分:1)
将您的方法更改为:
public void onProgressUpdate(TaskProgress... progress) {
pDialog.setProgress(progress[0].percentage);
pDialog.setMessage(progress[0].message);
}
...
很重要。
它期望的方法签名是
onProgressUpdate(TaskProgress... progress)
不是
onProgressUpdate(TaskProgress progress)