改变其用于通过alarmmanager的PendingIntent的意图 [英] change the intent of pendingintent which is used by an alarmmanager

查看:148
本文介绍了改变其用于通过alarmmanager的PendingIntent的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have set some alarm like:

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

 }

现在由于某种原因,我想更新触发报警的意图我。我有识别的欲望报警标识(标记)。我该怎么办呢?

now for some reason i want to update the Intent i of a triggered alarm. i have the id(tag) for identify the desire alarm. how can i do that ?

推荐答案

如果你想要做的是改变群众演员在意图,你可以像下面这样做

If all you want to do is change the extras in the Intent, you can do it like this:

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);

另外,如果你想改变任何东西在意图(如动作,或部件,或数据),你应该取消当前的报警,并创建一个新的一个像这样的:

Otherwise, if you want to change anything else in the Intent (like the action, or component, or data), you should cancel the current alarm and create a new one like this:

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

这篇关于改变其用于通过alarmmanager的PendingIntent的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆