在AsyncTask中更改ProgressDialog的消息

Lar*_*ars 0 android progressdialog android-asynctask

我有一个在AsyncTask中运行的ProgressDialog.我试图实现,只要缓冲区的长度更大,然后说10000,来自ProgressDialog的消息就会发生变化.

有人可以帮帮我吗,这可能吗?先感谢您.

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

缓冲区是在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();
    }
Run Code Online (Sandbox Code Playgroud)

SBe*_*413 5

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

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

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

   while ((line = reader.readLine()) != null) {
        sb.append(line);
        publishProgress(sb.length());
    }
Run Code Online (Sandbox Code Playgroud)

并摆脱那个Runnable.你不需要它.在这里查看AsyncTask的官方android文档:http: //developer.android.com/reference/android/os/AsyncTask.html 在这个页面上有一个很好的例子.