I have 2 buttons save and set alarm
and cancel alarm
which are meant to do exactly what they suggest.
Inside onCreate declared variables
final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
Code inside Cancel Button onClickListener
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) == null);
if (alarmUp)
{
new AlertDialog.Builder(AlarmActivity.this)
.setTitle("Alert")
.setMessage("This alarm will be deleted.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
alarmManager.cancel(sender1);
sender1.cancel();
Toast.makeText(getApplicationContext(), "Alarm Cancelled.", Toast.LENGTH_LONG).show();
}
})
.create().show();
}
else
{
new AlertDialog.Builder(AlarmActivity.this)
.setTitle("Alert")
.setMessage("Alarm for this is not set yet.")
.setPositiveButton("Ok",null)
.create().show();
}
Code for save button
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,
alarmintent,
PendingIntent.FLAG_NO_CREATE) == null);
if (alarmUp)
{
//Log.d("myTag", "Alarm is already active");
new AlertDialog.Builder(AlarmActivity.this)
.setTitle("Alert")
.setMessage("Already an alarm is set for this particular time and day.")
.setPositiveButton("OK",null
)
.create().show();
}
Calendar c = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis()+5000,24 * 60 * 60 * 1000, sender1);
When i click save
for the first time it shows
a http://www.4shared.com/download/puMWZEvRba/alert1.png
However as i have cancel
too i can click that to cancel the alarm.So i click cancel
button and it shows
a http://www.4shared.com/download/1UOTyVK0ce/alert2.png
which seems right.But when i again click save
button it shows
a http://www.4shared.com/download/puMWZEvRba/alert1.png
which means the cancel
button is not doing what it should do although it executes the toast
for this alarm will be deleted.
.Which again means there must be some problem with alarmManager.cancel(sender1)
.
Question
What to modify in the code to get the cancel
button work correctly?
P.S
I referred many posts like this but can't get what's the exact problem in my case.
Updated Code
For cancel button
final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,alarmintent,PendingIntent.FLAG_NO_CREATE) == null);
if (alarmUp)
{
new AlertDialog.Builder(AlarmActivity.this)
.setTitle("Alert")
.setMessage("This alarm will be deleted.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
alarmManager.cancel(sender1);
sender1.cancel();
Toast.makeText(getApplicationContext(), "Alarm Cancelled.", Toast.LENGTH_LONG).show();
}
})
.create().show();
}
else
{
new AlertDialog.Builder(AlarmActivity.this)
.setTitle("Alert")
.setMessage("Alarm for this is not set yet.")
.setPositiveButton("Ok",null)
.create().show();
}
For Save Button
final Intent alarmintent = new Intent(AlarmActivity.this, AlarmReceiver.class);
final AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
final PendingIntent sender1 = PendingIntent.getBroadcast(getApplicationContext(), 2, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,
alarmintent,
PendingIntent.FLAG_NO_CREATE) == null);
if (alarmUp)
{
//Log.d("myTag", "Alarm is already active");
new AlertDialog.Builder(AlarmActivity.this)
.setTitle("Alert")
.setMessage("Already an alarm is set for this particular time and day.")
.setPositiveButton("OK",null
)
.create().show();
}
Calendar c = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis()+5000,24 * 60 * 60 * 1000, sender1);
This results in same as previous.Cancel
button doesn't seem to work.
Solution
Thanks to @David Wasser i got it working now.Please see his answer.I also had to change
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,
alarmintent,PendingIntent.FLAG_NO_CREATE) == null)
to
boolean alarmUp = (PendingIntent.getBroadcast(AlarmActivity.this, 2,
alarmintent,PendingIntent.FLAG_NO_CREATE) != null)
in both the blocks.
But as developer.android.com says FLAG_NO_CREATE Flag indicating that if the described PendingIntent already exists, then simply return null instead of creating it.
I don't know about this issue!
See Question&Answers more detail:
os