2

我已经阅读了几乎所有关于使用 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;
        }
    }

我得到零错误或警告,我的代码运行良好,我的文件被保存,最终在异步任务完成后进度对话框被删除。但是当我使用 publishProgress 时,对话框消息根本不会更新。

我使用这篇文章作为指导,但正如您所看到的,即使给出了解决方案,OP 似乎也遇到了麻烦。

我究竟做错了什么?

谢谢1

4

1 回答 1

1

将您的方法更改为:

public void onProgressUpdate(TaskProgress... progress) {
   pDialog.setProgress(progress[0].percentage);
   pDialog.setMessage(progress[0].message);
}

...很重要。

它期望的方法签名是

onProgressUpdate(TaskProgress... progress)

不是

onProgressUpdate(TaskProgress progress)

于 2013-06-05T15:44:45.020 回答