我想使用AsyncTask和onProgressUpdate方法更新进度条。我在方法doInBackground中有循环,但是在循环结束后立即调用进度。 在publishUpdate行上方尝试添加日志并实时调用日志。
@Override
protected String doInBackground(Void... params) {
String message = "Inicialized";
JsonApiParser jsonApiParser = new JsonApiParser(NationalParks.getContext());
callList = getInicializationCallList();
int count = callList.size(), id;
for (int i = 0; i < count; i++) {
String call = callList.get(i);
String[] stringArray = call.split("/");
try {
id = Integer.parseInt(stringArray[stringArray.length - 1]);
} catch (NumberFormatException e) {
id = 0;
}
try {
if (call.contains("info")) {
JSONObject json = Request.getParkInfo(id);
jsonApiParser.insertPark(json);
} else if (call.contains("category-lodging")) {
JSONObject json = Request.getLodgingCategory(id);
jsonApiParser.insertLodgingCategory(json);
}
Log.e("call", call); // this called in real time
publishProgress((int) (((i + 1) / (float) count) * 100)); // this called after for cycle is ended
} catch (Exception e) {
message = e.getMessage();
e.printStackTrace();
return message;
}
}
SharedPreferences.Editor edit = NationalParks.getSharedPreferences().edit();
edit.putLong("lastUpdate", Calendar.getInstance().getTimeInMillis() / 1000);
edit.commit();
return message;
}
...
protected void onProgressUpdate(Integer... progress) {
Log.e("Progress", progress[0] + "%");
progressBar.setProgress(progress[0]);
}
知道为什么?