ProgressDialog不更新

时间:2014-09-28 12:57:24

标签: android progressdialog

我有一个从服务器下载文件的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个文件后进度停止时,对话框关闭。 折流板!

1 个答案:

答案 0 :(得分:0)

好的我修好了......

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

谢谢大家