【问题标题】:How to get the ID of the unique pendingIntent that is called by AlarmManager?如何获取AlarmManager调用的唯一pendingIntent的ID?
【发布时间】:2013-04-27 19:23:44
【问题描述】:

我正在构建一个警报应用程序。我现在添加的功能之一是闹钟的音量。用户在设置闹钟时,可以通过SeekBar选择闹钟音量。因此,当闹钟响起时,它的音量应该设置为用户设置的任何值。为此,我需要知道已调用了哪个特定警报。我已经用数据库中的警报 ID 识别了每个 PendingIntent。我的问题是一旦警报响起,我就是不知道如何再次检索此 ID。

如您所见,这就是我通过传入 alarm.getID() 来识别每个 PendingIntent 的方式:

PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(
ChangeAlarmActivity.this, (int)alarm.getID(), alarmIntent, 0);

所以在闹钟响起时调用的活动中,我想做类似的事情

int id = pendingIntent.getID();
Alarm alarm = database.getAlarm(id);
int volume = alarm.getVolume();

因此,如何在警报打开后调用的 Activity 中获取 PendingIntent 的唯一标识符?换句话说,我怎么知道哪个警报被触发了?

【问题讨论】:

    标签: android android-intent android-activity alarmmanager android-pendingintent


    【解决方案1】:

    首先,请原谅我的英语,它不是很好。 我认为您很可能已经找到了解决此问题的方法,但我也有同样的疑问,我无法轻易找出答案。因此,经过多次研究,我找到了一种方法。为了从触发的警报中检索 ID,可以将 ID 放在意图上:

    // create intent Intent intent = new Intent(context,MyClass.class); // store id intent.putExtra("id", yourId);

    在此之后,使用此意图创建 PendingIntent,用于设置警报。当警报触发时,onReceive(context, intent) 方法将接收用于创建 PendingIntent 的意图,因此可以在该方法上检索 id:

    // retrieves id long id = intent.getLongExtra("id", -1);

    希望对你有用!

    【讨论】:

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