Search code examples
javaandroidalarmmanagerandroid-pendingintentalarm

Unique PendingIntent with Manifest declaration


I am creating an Android application which programmatically creates alarms which persist when the application is closed. In order to have the alarms persist, I had to define the receiver in my Manifest like so:

<receiver
android:name="com.blah.blah.AlarmReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="999" >
     <action android:name="com.blah.blah.ALARM" />
</intent-filter>
</receiver>

Here is how I am currently creating the PendingIntents..

Intent intent = new Intent(ACTION_ALARM);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager manager = (AlarmManager   (this.getSystemService(Context.ALARM_SERVICE ));

Since I am using the constant ACTION_ALARM, all alarms are connected to the same PendingIntent, so calling .cancel() on that deletes all alarms. However, I want to delete specific alarm instances. The only way I know of to create a unique PendingIntent is to specify a unique action, which conflicts with defining my Receiver in the Manifest as I have above. Is there any other way to make distinguishable PendingIntents for my alarms? I tried adding Data as well, but it seemed the Receiver in the Manifest was not being triggered.

Alternatively, if I start a receiver for each alarm programmatically, is there a way to have those persist when the application is closed?

Thanks.


Solution

  • Found the solution here: How to create different pendingintent so filterEquals() return false?

    Using the second parameter of PendingIntent.getBroadcast works to establish a unique PendingIntent. There is no need for a unique Action, though that is what every other post I could find suggested.

    Not sure how to close the question now...