【问题标题】:ProgressDialog not updatingProgressDialog 没有更新
【发布时间】:2014-09-28 12:57:23
【问题描述】:

我有一个从服务器下载文件的 AsyncTask,我添加了代码来显示当前通过 publishProgress 下载的文件号。

在某些设备上,ProgressDialog 会按预期更新,因为在 doInBackground 中下载每个新文件时会更改文本。然而,在其他设备上,即使正在下载文件,它也只会显示第一条消息。在此期间进度微调器也无法转动。

只有两个真实设备很难确定操作系统级别是否是罪魁祸首。正在工作的设备是 2.3.3,而不能正确显示的设备是 4.2.2

ProgressDialog mProgressDialog;

....

public class OnLineUpdates2 extends AsyncTask<Void, String, Void> {

    @Override
    protected void onPreExecute(){
        mProgressDialog.setMessage("Please wait...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    };      

    protected void onProgressUpdate(final String... values) {  
        mProgressDialog.setMessage(values[0]);
    }

    @Override
    protected Void doInBackground(Void... params){   

        int local_last_update = prefs.getInt("lastupdate", 5); 
        mess="";

        while (local_last_update<onlineupdate){

            local_last_update = local_last_update +1;
            publishProgress("Extracting update " + local_last_update + "...");
            THIS LINE DISPLAYS ONLY ONCE ON SOME DEVICES

            //get updatefile
            StringBuilder total = new StringBuilder();
            try {

                Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(100);
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpGet httppost = new HttpGet("http://www.example.com/somefolder/updatefile" + local_last_update + ".txt" + "?unused=" + randomInt);
                HttpParams httpParameters = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
                HttpConnectionParams.setSoTimeout(httpParameters, 10000);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity ht = response.getEntity();


                BufferedHttpEntity buf = new BufferedHttpEntity(ht);
                InputStream is = buf.getContent();
                BufferedReader r = new BufferedReader(new InputStreamReader(is));

                String line;
                while ((line = r.readLine()) != null) {
                    if (line.length()>10){  //remove blank lines or lines with tabs
                        total.append(line + "\n");  
                    }
                }
                is.close();
                r.close();

                Log.d("test", "Downloaded the file " + local_last_update + ".txt");


            } catch (ConnectTimeoutException e) {
                Log.d("test", "ConnectTimeoutException\n" + e.toString());
                mess="Internet connection request timed out.";
                break;
            } catch (MalformedURLException e) {
                Log.d("test", "MalformedURLException\n" + e.toString());
                mess="On-line update file not found.";
                break;
            } catch (IOException e) {
                Log.d("test", "IOException\n" + e.toString());
                mess="Service Busy - Will try again later.";
                break;
            }

            if (total.toString().length()>10){
                 INSERT INTO DB.... 
            }                   
            SharedPreferences.Editor prefEditor = prefs.edit();
            prefEditor.putInt("lastupdate", local_last_update);
            prefEditor.commit();

            System.gc();

        }//while (local_last_update<onlineupdate){

        return null;
    }    

    @Override
    protected void onPostExecute(Void result){
        super.onPostExecute(result);

        mProgressDialog.cancel();


    };

}

我可能做错了什么的任何线索?

更新

刚刚在模拟器 API19 上尝试过,它在进度轮停止之前显示了前 8 个文件消息,并且没有显示其他消息,即使文件继续下载。当所有 37 个文件完成下载但进度在 8 个文件后停止时对话框关闭。 莫名其妙!

【问题讨论】:

  • 如何运行异步任务?
  • OnLineUpdates2 task2 = new OnLineUpdates2(); task2.execute();
  • 那么循环运行正常,但是UI没有更新?还有什么设备有这个问题?
  • Yes 循环运行良好,logcat 显示下载的文件,但 UI 卡在第一条消息上,进度轮静止。有问题的测试设备是三星 Galaxy Tab2... 还尝试在异步任务中使用 runOnUiThread 设置消息,同样的问题,
  • final String... values。 ???

标签: android progressdialog


【解决方案1】:

好的,我修好了……

每次成功下载后调用 System.gc()。显然会弄乱/混淆消息对话框的显示。我们生活和学习!

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多