서버에서 파일을 다운로드하는 AsyncTask가 있는데, publishProgress를 통해 현재 다운로드중인 파일 번호를 표시하는 코드가 추가되었습니다.ProgressDialog가 업데이트되지 않음
일부 장치에서는 ProgressDialog가 예상대로 업데이트되어 각각의 새 파일이 doInBackground에 다운로드 될 때마다 텍스트가 변경됩니다. 그러나 다른 장치에서는 파일을 다운로드하더라도 첫 번째 메시지 만 표시합니다. 또한이 시간 동안 진행률 회 전자가 돌아 가지 않습니다.
OS 레벨이 범인 인 경우 실제 장치를 두 개만 식별하기가 어렵습니다. 작동중인 기기가 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 개의 파일 이후에 진행이 중단되면 대화 상자가 닫혔습니다. 당황스러워!
어떻게 Asynctask를 실행합니까? – Blackbelt
OnLineUpdates2 task2 = 새로운 OnLineUpdates2(); task2.execute(); – Mark
루프가 제대로 실행되지만 UI가 업데이트되지 않습니까? 또한이 문제가 발생한 기기는 무엇입니까? – Kai