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.
Related
This is what I am using to set a alarm and it is visible when I fetch all the alarms set in the device.
/**
* Set alarm
**/
Intent alarmIntent = new Intent(context, LocalReminderReceiver.class);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long time = Long.valueOf("1526112720000");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, alarmIntent, FLAG_UPDATE_CURRENT);
manager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
/**
* Cancel alarm
*/
PendingIntent sender = PendingIntent.getBroadcast(context, 1, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
sender.cancel();
Am I missing something?
To set alarm use below line
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, intent, 0);
AlarmManager am = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
To Cancel alarm use below line
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,requestID, intent, 0);
am.cancel(pendingIntent);
Above both code for set and cancel alram is working for me.
You have to just pass same request id to set and cancel particular alarm
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 met same issue like this .
Delete alarm from AlarmManager using cancel() - Android
"
I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.
However when I call AlarmManager's cancel() method, the alarm isn't deleted."
In order to set :
Intent myIntent = new Intent(getApplicationContext(),
SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(),
pendingIntent);
In order to delete :
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(),
SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
But this doesn't remove an alarm registered.
Thanks in advance.
The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're using a different requestCode (zero instead of one).
For a quick fix, this should work:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent, 0);
alarmManager.cancel(pendingIntent);
In order to use PendingIntent.FLAG_UPDATE_CURRENT flag, see below:
Setting:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
Cancelling:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 1, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Initially for me also not worked,After Seeing many posts i realized that the pending intent to be canceled should be same as the original pending intent that was used to schedule alarm.
The pending intent to be cancelled should have set to same action and same data fields,if any have those were used to set the alarm.After setting the same ACTION and data values though i'm not using them,only cancelled the Alarm.
private void scheduleAlarms(Context context) {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);
String listOfIntervalConnection = Utils.getStringFromProperties(context, Properties.SP_LIST_OF_ENABLE_INTERVAL_CONNECTIONS, Properties.ENABLE_AFTER);
long enableAfter = DateUtils.MINUTE_IN_MILLIS * Long.parseLong(listOfIntervalConnection);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + enableAfter, enableAfter, pendingIntent);
}
and i try to cancel alarm like
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);
alarmManager.cancel(pendingIntent);
but sometimne is canceled sometimes still working. Why and how? What can i do?
You are using different instances of Intent in the PendingIntent when starting the service and while canceling it.
set the service intent as an instance variable and use it in both the starting and canceling of the service.
Try to use PendingIntent.FLAG_UPDATE_CURRENT instead of 0; Like this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, PendingIntent.FLAG_UPDATE_CURRENT);