【问题标题】:change the intent of pendingintent which is used by an alarmmanager更改警报管理器使用的挂起意图的意图
【发布时间】:2013-01-10 02:30:37
【问题描述】:

我设置了一些警报,例如:

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。我有用于识别欲望警报的 id(tag)。我怎样才能做到这一点 ?

【问题讨论】:

    标签: android alarmmanager android-pendingintent


    【解决方案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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多