Question

I have created the alarm as shown below

Intent intent = new Intent(this, Areceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, timenow, 3000, sender);

I created a button to stop the alarm. In the onclick method i have written the following code

Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
            0, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManagerstop.cancel(senderstop);

But the job is not stopping. What might be the issue?

Was it helpful?

Solution

You gave your Pending intent a request_code = 1234567 when you start the alarm. But you are using 0 when you try to stop it. Use this when trying to stop and should work

Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
            1234567, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManagerstop.cancel(senderstop);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top