使用publishProgress方法的AsyncTask无法更新进度对话框信息 [英] Cannot update Progress Dialog Message in AsyncTask using publishProgress method

查看:1310
本文介绍了使用publishProgress方法的AsyncTask无法更新进度对话框信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了几乎每一个岗位上读到这里如何使用publishProgress来设置进度对话框一个新的消息,我根本无法得到它的工作。

I have read through nearly every post on here about using publishProgress to set a new message on a progress dialog and I simply cannot get it to work.

code:

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

我得到的零错误或警告,我的code运行正常,与正在救了我的文件,并最终异步任务完成后,被删除的进度对话框。但是对话的消息,只是惯于更新,当我使用publishProgress。

I get zero errors or warnings, my code runs fine, with my files being saved and eventually the progress dialog being removed after the Async task is complete. but the dialog message simply wont update when I use publishProgress.

我用<一个href=\"http://stackoverflow.com/questions/7970113/change-a-dialogs-message-while-using-asynctask\">this帖子为指导,但你可以看到,它看起来像OP有被赋予了解决方案,即使麻烦了。

I used this post as a guide, but as you can see, it looks like the OP had trouble even after a solution was given.

我在做什么错了?

Thanks1

推荐答案

更改方法:

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

... 是很重要的。

,预计该方法签名

onProgressUpdate(TaskProgress ...进度)

不是

onProgressUpdate(TaskProgress进度)

这篇关于使用publishProgress方法的AsyncTask无法更新进度对话框信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆