Johnathan Au
Johnathan Au

Reputation: 5362

How to get the ID of the unique pendingIntent that is called by AlarmManager?

I am building an alarm application. One of the features I am adding right now is the volume of the alarm. When the user is setting up the alarm, they can choose the volume of the alarm with a SeekBar. So, when the alarm goes off, the volume of it should be set to whatever the user set it to. To do this, I would need to know which specific alarm has been called. I have identified each PendingIntent with the id of the alarm from the database. My problem is I just don't know how to retrieve this id again once the alarm goes off.

As you can see, this is how I identify each the PendingIntent by passing in alarm.getID():

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

So in my activity that is called when the alarm goes off I want to do something like

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

How do I therefore get the unique identifier of a PendingIntent within an Activity that is called after the alarm is turned on? In other words, how do I know which alarm is being set off?

Upvotes: 2

Views: 2480

Answers (1)

Spes
Spes

Reputation: 51

In first place, forgive me for my english, it's not very good. I think that you most likely already found out a solution for this issue, but I had the same doubt and I couldn't figure out the answer easily. So after many researches I figure out a way. For retrieve a ID from the alarm fired is possible put a ID on intent:

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

And after this, use this intent for create the PendingIntent, that going to setting up the alarm. When the alarm fire off, the method onReceive(context, intent) will receive the intent used for create the PendingIntent, thus the id can be retrieve on the method:

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

I hope that be useful!

Upvotes: 1

Related Questions