每 5 分钟在后台调用一些方法
call some method in background every 5 mins
你好,我试图在后台服务中每 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
使用
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm that has inexact trigger time requirements;
for example, an alarm that repeats every hour, but not necessarily at
the top of every hour.
将 alarmMgr.set 替换为 alarmMgr.setInexactRepeating
你好,我试图在后台服务中每 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
使用
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.
将 alarmMgr.set 替换为 alarmMgr.setInexactRepeating