Currently I've got this code:
public static void setupAlarm(Context context) {
Intent myIntent = new Intent(context, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, myIntent, PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) {
return;
} else {
pendingIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_RETRY, myIntent,
PendingIntent.FLAG_ONE_SHOT);
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
What I want is to use pending intent one time and wait for the fire. If in the meantime someone asks for a new alarm, if the alarm exist I don't want to setup anything. Now my question: after the first alarm, the pending intent is deleted due to the ONE_SHOT flag, but can I create the pending intent again or not?
1- requestCode is used to get the same pending intent later on (for cancelling etc) 2- Yes, they will get override as long as your specify the same Receiver to your Intent that you specify on your PendingIntent.
Sticky Intent : sticky intents are associated with the android system for the future broadcast events. Pending Intent : Those intent which you want to trigger at some time in future when you application is not alive.
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int) , getBroadcast(Context, int, Intent, int) , and getService(Context, int, Intent, int) .
Yes, of course you can create it again. You will get a different PendingIntent
from the first one.
However, there are a few problems with the code you've posted. First of all, you create the PendingIntent
like this:
pendingIntent = PendingIntent.getBroadcast(context,
PENDING_INTENT_RETRY, myIntent, PendingIntent.FLAG_ONE_SHOT);
but you check if it exists like this:
pendingIntent = PendingIntent.getBroadcast(context,
0, myIntent, PendingIntent.FLAG_NO_CREATE);
This check will always return null because you are using a different requestCode
! When you create the PendingIntent
you pass PENDING_INTENT_RETRY
as requestCode
, but when you check if it exists you pass 0 as requestCode
.
The second problem is the way FLAG_ONE_SHOT
works. If you create a PendingIntent
using FLAG_ONE_SHOT
and then try to get a PendingIntent
using FLAG_NO_CREATE
, it will always return null, even if the PendingIntent
has not yet been used! Because of this behaviour, you cannot use FLAG_NO_CREATE
to determine if an alarm is pending, if you have set that alarm using a PendingIntent
created with FLAG_ONE_SHOT
.
If you really want to use this architecture, then you cannot use FLAG_ONE_SHOT
. Just create the PendingIntent
normally (without flags) and check for its existence using FLAG_NO_CREATE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With