问题
I have checkboxlist where the user selectes some items I am envloping his selecting in json format thenI am firing the json string from the alarmManager to the GetLLRD class. Currently I am getting intent in the OnHandleIntent not every 30 second but roughly every second or several times in one second. How can I manage it to get the intent just once every 30 seconds in the onHandleIntent in the GetLLRD class?
Some of the output:
07-07 17:18:01.817: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:02.828: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:04.049: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:04.810: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:05.821: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:06.822: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:07.822: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:08.843: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:09.824: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:10.835: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:11.876: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
07-07 17:18:12.907: I/System.out(6493): test from the onHandleIntent{"selected":[6,9]}
MainActivity class:
Intent intent = new Intent(MainActivity.this,
GetLLRD.class);
intent.putExtra("json_data", json);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(getApplicationContext(), 3,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), 30 * 1000, pendingIntent);
startService(intent);
GetLLRD class:
public class GetLLRD extends IntentService {
public GetLLRD() {
super("IntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
String jSONString = intent.getStringExtra("json_data");
System.out.println("test from the onHandleIntent" + jSONString);
if(jSONString != null){
System.out.println("Test");
}
}
}
When I try to receive the intent in the IntentReceiver
then I am getting it every 30 seconds but in the onHandleIntent I am receiving it too quick as I mentioned earlier.
IntentReceiver:
public class IntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getStringExtra("json_data");
if (!action.isEmpty()) {
System.out.println("test from IntentReiceier" + action);
}
} catch (Exception e) {
}
}
}
来源:https://stackoverflow.com/questions/31272730/onhandleintent-receiving-the-fired-intent-every-second-instead-of-every-30-secon