Reputation: 3389
I am working on a simple AlarmManager example, and have run into an interesting problem. I am setting a boolean value to be passed through our intent based on a button that is clicked. There is a "repeat alarm" and "one time alarm" button that tells us whether or not we are supposed to repeat our Alarm or have a one time Alarm, I am utilizing the ability to pass extra values with intents (intent.putExtra("key","value");), nothing new, nothing special. The problem I am having is that once the "key" has been set once, it can't be reset.
I have a MainActivity that includes the line
AlarmReceiver alarm = new AlarmReceiver();
within that activity, there are 2 buttons. If I click the "repeat alarm" button, I call
alarm.SetAlarm(context);
which is the method inside of my AlarmReceiver class:
// Set our repeating alarm
public void SetAlarm(Context context){
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent repeatIntent = new Intent(context, AlarmReceiver.class);
repeatIntent.putExtra("one_time", false); // this should change our boolean
PendingIntent pi = PendingIntent.getBroadcast(context, 0 , repeatIntent, 0);
// Set to go off after 5 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi);
}
and if I click the "one time alarm" button, I call
alarm.SetOneTimeAlarm(context);
which is the method in of my AlarmReceiver class:
// Set our one time alarm
public void SetOneTimeAlarm(Context context){
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent oneTimeIntent = new Intent(context, AlarmReceiver.class);
oneTimeIntent.putExtra("one_time", true); // this should change our boolean
PendingIntent pi = PendingIntent.getBroadcast(context, 0, oneTimeIntent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
However, within my onReceive() method of my AlarmReceiver class:
Bundle extras = intent.getExtras();
String messageString = "";
boolean oneTime = extras.getBoolean("one_time", false);
if(extras != null && oneTime == true){
messageString = messageString + "One time Timer : ";
}
else{
messageString = messageString + "Repeating Timer : ";
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
messageString = messageString + formatter.format(new Date());
Toast.makeText(context, messageString, Toast.LENGTH_LONG).show();
The problem is that if I click on the "one time alarm" button, the "one_time" boolean is set to true and stays set to true even if I click the "repeat alarm" button and try to set the "one_time" boolean to false (and vice versa). Is there an issue with overriding extras from intents? Are they saved somewhere and accessed differently or something? Just not sure why I can't change the value once it is set.
Upvotes: 0
Views: 568
Reputation: 4620
try changing this:
PendingIntent pi = PendingIntent.getBroadcast(context, 0 , repeatIntent, 0);
to this:
PendingIntent pi = PendingIntent.getBroadcast(context, 0 , repeatIntent, PendingIntent.FLAG_UPDATE_CURRENT);
The problem is probably that your intent is not refreshed when new alarm is fired.
Upvotes: 3
Reputation: 2330
Try and use -
getIntent().removeExtra("one_time");
and the Re-set it using your initial code -
repeatIntent.putExtra("one_time", false);
I am not sure since I never looked into it before but Bundle might be a mutable object (by the behavior you are describing).
Upvotes: 0