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?
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);
Related
I set my alarm receiver as follow:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("alarmId", REQUEST_CODE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
time * 1000, pendingIntent);
Then I am canceling it from another class:
Intent myIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)activity.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
But it does not work. Please help me!
Your Intents are not equivalent. The first one uses REQUEST_CODE. The second one uses 0. You need to make these be the same.
this is how i am starting a repeating service.
Intent intent2 = new Intent(Main_page.this, SyncService.class);
final AlarmManager alarm2 = (AlarmManager)
Main_page.this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pintent2 = PendingIntent.getService(Main_page.this, 0, intent2, 0);
alarm2.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 2 * 60000, pintent2);
is there any chance of alarm stops repeating.
You can cancel an intent like:
Intent intent = new Intent(context, AlarmService.class);
PendingIntent pIntent = PendingIntent.getService(context, model.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
I'm beginner in android,and simple run the alarm manager in mainActivity with this code:
Intent intent = new Intent(testSendWithFood.this, AlarmReciever.class);
intent.putExtra("key", "Alert");
//PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 10, 40 * 1000, pendingIntent);
and i want in another activity kill that alarm manager with this code:
Intent intent = new Intent(this, AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
but so alarm manager not kill!,why?thanks.
The pending intent has to be the same. So if you change the intent id to 1253 when you create the intent and then use the same to cancel the pending intent so change
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),0, intent, 0);
To
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1253, intent, 0);
i have a service(AlarmService.java) where the alarm is being declared:
Intent intentAlarm = new Intent(this, TimeAlarm.class);
intentAlarm.putExtra("Notif_body", Notif_Body);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+5*1000, displayIntent);
and an activity(user_settings.java) from where I want to delete that alarm once a checkbox is unchecked:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intentAlarm = new Intent(this,TimeAlarm.class);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, 0);
alarmManager.cancel(displayIntent);
but it's not working my alarm is not being deleted why is that although the pendingintent is the same as it should be!!
intentAlarm.putExtra("Notif_body", Notif_Body);
Put the above line while creating the intentAlarm during cancelling the alarm or remove it from the time when you are setting the alarm. I guess this would work.
My problem was :
instead of adding:
if(bool_activate_reminder==true){
//call the alarm method;
}
I was doing:
if(bool_activate_reminder=true){//with one equal
//call the alarm method;
}
so it was always being accessed... when I fixed it and used the following code in the service:
Intent intentAlarm = new Intent(getApplication(), TimeAlarm.class);
//add the notification body to the intent:
intentAlarm.putExtra("Notif_body", Notif_Body);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set the alarm for particular time
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,new GregorianCalendar().getTimeInMillis()+20*1000, displayIntent);
and in the activity:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intentAlarm = new Intent(getApplication(),TimeAlarm.class);
PendingIntent displayIntent = PendingIntent.getBroadcast(getApplication(),1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(displayIntent);
it worked just fine..
Hi I'm trying to cancel AlarmManager. I wrote some test code but It,s not working, my code:
Alarm creation:
Intent myIntent = new Intent(this,NotificationService.class);
PendingIntent pendingIntentFriday = PendingIntent.getService(this,123098,myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,nextFriday.getTimeInMillis() , pendingIntentFriday);
Alarm cancel:
Intent stopIntent = new Intent(this,NotificationService.class);
PendingIntent stopFriday = PendingIntent.getService(this,123098,stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager stopManager = (AlarmManager) getSystemService(ALARM_SERVICE);
stopManager.cancel(stopFriday);
Try changing your flag to "FLAG_UPDATE_CURRENT"
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReceiverForCancelling.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
service.cancel(pending)