改变其用于通过alarmmanager的PendingIntent的意图(change the in

2019-08-05 04:31发布

我已经设置了一些报警,如:

public void SetAlarm(Context context, int tag, long time){
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     i.putExtra("position", tag);
     PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
     am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute

 }

现在由于某种原因,我想更新的Intent i触发报警。 我有识别的愿望报警标识(标签)。 我怎样才能做到这一点 ?

Answer 1:

如果你想要做的是改变演员的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


文章来源: change the intent of pendingintent which is used by an alarmmanager