我有checkboxlist,用户选择了一些我喜欢用json格式选择的项目然后我从alarmManager向GetLLRD类发出json字符串。目前我对OnHandleIntent的意图不是每30秒,而是大约每秒或几秒钟。如何管理它以在GetLLRD类的onHandleIntent中每30秒获取一次意图?
部分输出:
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类:
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课程:
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");
}
}
}
当我尝试接收IntentReceiver
中的意图时,我每隔30秒就会收到一次意图,但是在onHandleIntent中,我收到的速度太快,如前所述。
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) {
}
}
}