我想在完成後回調相同的asynctask,我該怎麼做。我想定期從服務器同步我的消息。目前我使用計時器定期運行該方法,但它不值得,所以我想它的完成從第一次執行的android異步任務回調
這裏之後再打這個方法是我異步任務功能做
private class syncMessageFromServer extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
// setting progress bar to zero
//progressBar.setProgress(0);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
@SuppressWarnings("deprecation")
private String uploadFile() {
String str = "";
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://192.168.1.2/AndroidApp/GetMessage?loginUserInfoId="+loginUserInfoId+"&recieverUserInfoId="+receiverUserInfoId+"&isPendingToSynce="+true);
try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
JSONArray jArray = new JSONArray(str);
for(int i = 0; i<=jArray.length()-1; i++)
{
JSONObject row = jArray.getJSONObject(i);
ChatMessage cm = new ChatMessage();
String onlineFileURL = "";
String upFileURL = row.getString("FileAttachedURL").replace(" ", "%20");
String offlineFileURL = "";
if(!upFileURL.isEmpty()) {
onlineFileURL = "http://192.168.1.2" + row.getString("FileAttachedURL").replace(" ", "%20");
downloadBitmap(onlineFileURL);
String fileName = upFileURL.substring(upFileURL.lastIndexOf('/') + 1, upFileURL.length());
offlineFileURL = Environment.getExternalStorageDirectory() + File.separator + "/Planetskool/Media/Images/" + fileName;
}
else {
offlineFileURL = "";
}
/******* Firstly take data in model object ******/
cm.setOriginalMsgThreadId(row.getString("MessageThreadId"));
cm.setSenderUserInfoId(row.getString("SenderUserId"));
cm.setReceiverUserInfoId(row.getString("MultipleReceiversId"));
cm.setMessageStatus("SENT");
cm.setIsPending(0);
cm.setMessageText(row.getString("MessageText"));
cm.setMediaURL(offlineFileURL);
cm.setThumbImage(offlineFileURL);
cm.setMediaMIMEType("");
cm.setMediaSize(0);
cm.setMediaName("");
cm.setLatitude("");
cm.setLongitude("");
cm.setSendTimeStamp(row.getString("SendTime"));
cm.setReceiveTimeStamp(row.getString("ReadTime"));
long messageThreadId = db.SendMessage(cm);
confirmSyncedToServer(row.getString("MessageId"));
chatMessageAdapter.add(cm);
}
} catch (JSONException e) {
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
new syncMessageFromServer.execute();
super.onPostExecute(result);
}
}
使用服務,而不是定時運行同步任務定期 – 2015-02-11 05:47:58
檢查我接受的答案http://stackoverflow.com/questions/23124414/android-auto-refresh-when-new-data-inserted-into-listview/23124493 #23124493 – Boopathi 2015-02-11 06:24:48