如果您只想更改 中的附加功能Intent
,您可以这样做:
Intent i = new Intent(context, Alarm.class);
// Set new extras here
i.putExtra("position", tag);
// Update the PendingIntent with the new extras
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i,
PendingIntent.FLAG_UPDATE_CURRENT);
否则,如果您想更改其中的任何其他内容Intent
(例如操作、组件或数据),您应该取消当前警报并创建一个新警报,如下所示:
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
// Extras aren't used to find the PendingIntent
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i,
PendingIntent.FLAG_NO_CREATE); // find the old PendingIntent
if (pi != null) {
// Now cancel the alarm that matches the old PendingIntent
am.cancel(pi);
}
// Now create and schedule a new Alarm
i = new Intent(context, NewAlarm.class); // New component for alarm
i.putExtra("position", tag); // Whatever new extras
pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
// Reschedule the alarm
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute