【问题标题】:Change the message of a ProgressDialog in AsyncTask在 AsyncTask 中更改 ProgressDialog 的消息
【发布时间】:2011-10-29 11:07:02
【问题描述】:

我有一个在 AsyncTask 中运行的 ProgressDialog。 我试图实现这一点,只要缓冲区的长度大于 10000,ProgressDialog 中的消息就会发生变化。

谁能帮帮我,这可能吗? 提前谢谢你。

@Override
    protected void onProgressUpdate(Integer... progUpdate) { 
         if (progUpdate[0] >= 10000){ 
            progress.setMessage("Informatie wordt opgehaald...."); 
        } 
     } 

缓冲区是在 AsyncTask doInBackGround 中创建的:

try { 
        HttpResponse response = httpClient.execute(request); 

        System.out.println("Response: " + response.getEntity().getContentLength());

        /******* READ CONTENT IN BUFFER *******/
        InputStream inputStreamActivity = response.getEntity().getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreamActivity));
        StringBuilder sb = new StringBuilder();
        String line = null;
        int count = sb.length();

        while ((line = reader.readLine()) != null) {
            sb.append(line);
             publishProgress(sb.length());

        }

        /******* CLOSE CONNECTION AND STREAM *******/
        System.out.println(sb);
        inputStreamActivity.close();
        kpn = sb.toString();

        httpClient.getConnectionManager().shutdown();
    }

【问题讨论】:

    标签: android android-asynctask progressdialog


    【解决方案1】:

    要更改对话框的消息,您需要使用 AsyncTask 的 onProgressUpdate 方法并将 AsyncTask 的第二个参数定义为整数。 onProgressUpdate 看起来像:

    protected void onProgressUpdate(Integer... progUpdate) {
         if (progUpdate[0] >= 10000){  // change the 10000 to whatever
            progress.setMessage("The new message");
        }
     }
    

    要调用它,您需要在 AsyncTask 的 doInBackground 方法中更新这些行:

       while ((line = reader.readLine()) != null) {
            sb.append(line);
            publishProgress(sb.length());
        }
    

    然后摆脱那个 Runnable。你不需要它。在此处查看 AsyncTask 的官方 android 文档:http://developer.android.com/reference/android/os/AsyncTask.html 该页面上有一个很好的示例。

    【讨论】:

    • 嗨 SBerg413,谢谢回复,我更改了代码(已编辑),但似乎没有更改消息。请你看一眼好吗?
    • 不确定你用那个 for 循环做了什么,但什么也没做。我删除了它并将 publishProgress 调用添加到您的 while 循环中。看看吧。
    猜你喜欢
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2011-04-26
    相关资源
    最近更新 更多