您好我正在尝试在后台服务中每隔5分钟从我的应用程序向服务器发送一些字符串,但在我的服务器中我可以看到它每3秒发送一次数据。这肯定是错误的逻辑,但我看不到它。有人可以帮我吗? 感谢
public class MyClass extends IntentService{
public MyClass(){
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "The service has now started");
setALarm();
if(isNetworkAvailable()){
sendLog();
Toast.makeText(this, "Logs Sent!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Network is NOT avail!"+DateFormat.getDateTimeInstance().format(new Date()),Toast.LENGTH_LONG).show();
}
}
public void sendLog(){
final String CLASS_NAME = "MujCLASSname";
String LOG = "Network is avail!"+DateFormat.getDateTimeInstance().format(new Date());
Item item = new Item();
if(!LOG.equals("")){
item.setName(LOG);
item.save().continueWith(new Continuation<IBMDataObject, Void>() {
@Override
public Void then(Task<IBMDataObject> task) throws Exception {
if (task.isCancelled()){
Log.e(CLASS_NAME, "Exception : Task " + task.toString() + " was cancelled.");
}
// Log error message, if the save task fails.
else if (task.isFaulted()) {
Log.e(CLASS_NAME, "Exception : " + task.getError().getMessage());
}
// If the result succeeds, load the list.
else {
MainActivity ls = new MainActivity();
ls.listItems();
}
return null;
}
});
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
public void setALarm(){
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyClass.class);
PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP,
1000*60*5*2, alarmIntent);
}
}
08-17 21:10:34.924 15574-15660/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:10:50.333 15574-16100/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:14:48.056 15574-18056/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:14:52.512 15574-18185/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:15:14.354 15574-18378/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:15:18.538 15574-18409/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:17:17.238 15574-19324/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:19:47.312 15574-20394/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:20:19.477 15574-20640/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:20:19.626 15574-20646/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:22:19.987 15574-22298/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:24:47.416 15574-23660/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
08-17 21:24:48.831 15574-23688/com.ibm.bluelist I/com.ibm.bluelist﹕ The service has now started
答案 0 :(得分:1)
使用
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
安排具有不准确触发时间要求的重复警报; 例如,每小时重复一次的警报,但不一定是 每小时的最高点。
用alarmMgr.setInexactRepeating
替换alarmMgr.set