2014-09-28 81 views
0

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

在某些设备上,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(); 


    }; 

} 

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

UPDATE

刚在模拟器API19和之前的进度轮停止并显示任何其他消息,尽管文件仍然被下载了它显示的第一个8个文件的消息。当所有37个文件完成下载后关闭对话框,但8个文件后停止进度。 挡板!

+0

你如何运行Asynctask? – Blackbelt 2014-09-28 13:00:19

+0

OnLineUpdates2 task2 = new OnLineUpdates2(); task2.execute(); – Mark 2014-09-28 13:04:32

+0

所以循环运行正常,但UI不更新? 此外,有什么问题的设备? – Kai 2014-09-28 13:30:41

回答

0

好吧我固定它...

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

谢谢你们伙计