• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Disable alarms with AlarmManager

fabiobh

Lurker
Hi folks, I'm creating a code that generates an alarm that rings several times in a given time interval.

[java]
buttonStart.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(Alarm2Activity.this,
MyAlarmService.class);
pendingIntent = PendingIntent.getService(Alarm2Activity.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 5000,
pendingIntent);
Toast.makeText(Alarm2Activity.this, "Start Alarm",
Toast.LENGTH_LONG).show();
}
});
[/java]
And use the code below to disable the alarm:
[java]
buttonCancel.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Toast.makeText(Alarm2Activity.this, "Cancel!",
Toast.LENGTH_LONG).show();
}
});
[/java]
But when I start the alarm and leave the application, the alarm continues, so far so good, but when I go back into the application and try to disable the alarm by clicking the cancel button and it does not disable.

The alarm is still running, it only disable, when I start again, and then click in the cancel button.

My problem here is that once you exit the application and come back, I'm not able to retrieve the alarm, I can create another just over and then cancel.

How can I fix this?
 
Back
Top Bottom