Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop the setrepeat of Android alarmmanager

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?

like image 384
sankara rao bhatta Avatar asked Jun 17 '11 16:06

sankara rao bhatta


1 Answers

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);
like image 54
bytebender Avatar answered Nov 15 '22 19:11

bytebender