我们如何设置一个AlarmManager
, 为一个ContentObserver
类 in PendingIntent
?
可能的方法是:
PendingIntent.getActivity
PendingIntent.getService
PendingIntent.getBroadCast
那么,如果可能的话,应该使用哪一个呢?
我们如何设置一个AlarmManager
, 为一个ContentObserver
类 in PendingIntent
?
可能的方法是:
PendingIntent.getActivity
PendingIntent.getService
PendingIntent.getBroadCast
那么,如果可能的话,应该使用哪一个呢?
PendingIntent.getBroadCast 是您所遇到问题的正确答案。你可以使用如下
Intent alarm_intent = new Intent(getApplicationContext(),
AlarmReceiver.class);
// In reality, you would want to have a static variable for the request
// code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837,
alarm_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 15);
long interval = 900 * 1000; //
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
interval, sender);