Sunday, September 28, 2014

Android : ProgressDialog not updating



I have an AsyncTask that downloads files from a server, I've added code to display the file number that is currently being downloaded via publishProgress.


On some devices the ProgressDialog updates as expected changing the text as each new file is downloaded in doInBackground. However on other devices it only ever displays the first message even though the files are being downloaded. Also the progress spinner fails to turn during this time.


Only having two real devices it's hard to identify if OS level is the culprit. The device that is working is 2.3.3 and the one that does not display correctly is 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();


};

}


Any clues to what I may have done wrong?


No comments:

Post a Comment