我希望我的SyncMessageFromServer()在延迟5000后执行,当SyncMessageFromServer()成功执行时;
我的功能如下
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
SyncMessageFromServer();
}
}, timer);
private void SyncMessageFromServer() {
//Toast.makeText(getApplicationContext(), "FromServer Run", Toast.LENGTH_LONG).show();
if(isInternetOn() && loginUserInfoId != "") {
String str = "";
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://192.168.1.2/AndroidApp/GetAllMessage/" + loginUserInfoId);
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);
if (row.getString("SenderUserId") != loginUserInfoId) {
ChatMessage cm = new ChatMessage();
String onlineFileURL = "";
String fileType = "";
String upFileURL = row.getString("FileAttachedURL").replace(" ", "%20");
String offlineFileURL = "";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
if (upFileURL.contains(".mp4")) {
fileType = "VIDEO";
otherFileName = "VID_" + timeStamp + ".mp4";
onlineFileURL = "http://192.168.1.2" + row.getString("FileAttachedURL").replace("OutputVideo/", "");
timer = 60000;
new DownloadFileFromURL().execute(onlineFileURL);
offlineFileURL = Environment.getExternalStorageDirectory() + File.separator + "/Planetskool/Media/Videos/" + otherFileName;
}
else {
fileType = "";
offlineFileURL = "";
timer = 3000;
}
/******* 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.setIsPendingToUpdate(1);
cm.setMessageText(row.getString("MessageText"));
cm.setMediaURL(offlineFileURL);
cm.setThumbImage(offlineFileURL);
cm.setMediaMIMEType(fileType);
cm.setMediaSize(0);
cm.setMediaName("");
cm.setLatitude("");
cm.setLongitude("");
cm.setSendTimeStamp(row.getString("SendTime"));
cm.setReceiveTimeStamp(timeStamp);
long messageId = db.SendMessage(cm);
//Log.d("MyService", "ChatMessageId = " + messageId);
Intent iMla = new Intent("REFRESH_DATA");
iMla.putExtra("id", messageId);
//intent.putExtra("id", messageId);
sendBroadcast(iMla);
String notiAvatarURL = "";
String notiUserName = "";
List<ChatMessageThread> allChatMessageThread = db.GetMessageThreadsById(row.getString("SenderUserId"));
for (ChatMessageThread chatMessageThread : allChatMessageThread) {
notiAvatarURL = chatMessageThread.getMessageThreadAvatarURL();
notiUserName = chatMessageThread.getMessageThreadTitle();
}
if (row.getString("SenderUserId") != loginUserInfoId) {
displayNotificationMessage(row.getString("MessageText"), notiAvatarURL, notiUserName);
}
confirmSyncedToServer(row.getString("MessageId"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
SyncMessageFromServer();
//SyncPendingMessageToServer();
}
}, timer);
}
我希望在完成执行后定期执行此功能,如果我给它延迟5000毫秒然后它完全下载我的视频它只下载它可以在5000毫秒下载多少
答案 0 :(得分:-1)
以这种方式使用来调用每5秒调用一次方法
public void scheduleEvery5Sec() {
handler.postDelayed(new Runnable() {
public void run() {
// call method here
SyncMessageFromServer();
handler.postDelayed(this, 5000);
}
}, 5000);
}