PendingIntent For Multiple Alarms - android

StartAlarmMethod();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 , pi);
I am using PendingIntents for multiple Alarms. How can i properly use TAGS, so i can later cancel only the Alarm that i don't want to use anymore?
Right now i'm seting Alarms with the code above. But if i set more than 1, using the code appears bellow i stop all upcoming Alarms. Instead of that i want to be able to identify somehow this PendingIntents and cancel only the ones that are not required.
CancelMethod();
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);

To support multiple alarms send a different request code each time you call it in the getBroadcast method. To cancel just send the same request code.
Below code you can loop it within for.
Note: replace i with any value and when cancelling pass the same value to cancel the particular alarm.
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, alarmIntent, 0);
manager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent);
To cancel, send the requestcode which you want to cancel,
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, i, intent, 0);
alarmManager.cancel(sender);

Related

Android : Unable to cancel alarm using AlarmManager

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

Alarm manager is not working as expected

Basically, I want to create two alarms:
Fire daily at 6 PM
Fire monthly at a specific date at 4 PM.
But issues are after executing monthly alarm first one is also executing at 4 PM.
Following way I am creating alarm:
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
// For 1st alarm
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// For 2nd alarm
calendar1.set(2017,8,17,16,00,00);
Bundle bundle = new Bundle();
Intent intent = new Intent(context, AlarmReceiver.class);
bundle.putInt("NotificationId1", 1);
bundle.putInt("NotificationId2", 2);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
alarm.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent1);
And after firing monthly alarm I am again defining alarm with the further date.
Issue is after firing monthly alarm first alarm is executing at 4 pm. How to resolve the issue?
Any kind of help will be appreciated.
your first alarm is repeating so create alarm like these to set alarm.
if(Build.VERSION.SDK_INT < 23){
if(Build.VERSION.SDK_INT >= 19){
setExact(...);
}
else{
set(...);
}
}
else{
setExactAndAllowWhileIdle(...);
}
now in your broadcast receiver set next alarm to fire for next day at 4pm
Try this code, I hope you will get your answer.
Bundle bundle1 = new Bundle();
Intent intent1 = new Intent(context, AlarmReceiver.class);
bundle1.putInt("NotificationId1", 1);
intent1.putExtras(bundle1);
Bundle bundle2 = new Bundle();
Intent intent2 = new Intent(context, AlarmReceiver.class);
bundle2.putInt("NotificationId2", 2);
intent2.putExtras(bundle2);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
Make your both the alarms separate from each other. For this you need 2 intents, 2 pending intent and 2 alarm managers. You can take a reference from - this link
your code should be like-
// define variables
private AlarmManager alarmMgr1;
private AlarmManager alarmMgr2;
private PendingIntent pendingIntent1;
private PendingIntent pendingIntent2;
private Calendar timeFirst;
private Calendar timeSecond;
private Context context;
// init variables
timeFirst = Calendar.getInstance();
timeSecond = Calendar.getInstance();
timeFirst.set(Calendar.HOUR_OF_DAY, 18);
timeFirst.set(Calendar.MINUTE, 0);
timeFirst.set(Calendar.SECOND,0);
timeFirst.set(Calendar.MILLISECOND,0);
timeSecond.set(2017,8,17,16,00,00);
// set values
alarmMgr1 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr2 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent myIntent1 = new Intent(context, AlarmReceiver.class);
pendingIntent1 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent myIntent2 = new Intent(context, AlarmReceiver.class);
pendingIntent2 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr1.setRepeating(AlarmManager.RTC_WAKEUP, timeFirst.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);
if (Build.VERSION.SDK_INT >= 23){
alarmMgr2.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
else if (Build.VERSION.SDK_INT >= 19){
alarmMgr2.setExact(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
else{
alarmMgr2.set(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
}
hope it will be helpful and give you a better idea.
You need 2 intent.
Because you are sharing the same intent, any alarm running will also call the same intent.
I hope this helps.

Remove old alarm and set a new one with Alarm manager

I am having a issue with alarm manager. I need to remove old alarm set to a particular time and reset the alarm to new time. I have tried many scenarios and none working for me. Any help should be greately appreciated.enter code here
// To remove alarm
am = (AlarmManager) getContext().getSystemService( ALARM_SERVICE);
Intent i = new Intent( getContext(),AlarmReceiver.class);
PendingIntent p = PendingIntent.getBroadcast( getContext(), profileFragmentRequestCode, i, 0);
am.cancel(p);
p.cancel();
// To set new alarm
am = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), profileFragmentRequestCode, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 55);
if (android.os.Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
Make sure you have used same REQUEST_CODE with your PendingIntent both for setting and canceling alarm.
SET ALARM:
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent)
CANCEL ALARM:
AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);

How to edit and delete alarm time in Android?

I have built an alarm which works properly:
public static void setAlarm(int hours, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
Now, how can I edit the alarm time and delete the alarm?
For cancelling the alarm you may call AlarmManager.cancel with your PendingIntent.
Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
alarmManager.cancel(pendingIntent);
For editing the alarm, you have the option to first cancel it and re-add it or you may add PendingIntent.FLAG_UPDATE_CURRENT as the last option when scheduling a new alarm.
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0, PendingIntent.FLAG_UPDATE_CURRENT);
Possible duplicate Cancel an AlarmManager pendingIntent in another pendingintent

cancel the scheduled android notification

I have scheduled android notifications using alarm manager but I couldn't cancel the notification where I tried to cancel it using this notificationManager.cancelAll(); does not work and then I tried to cancel alarm manager itself, unfortunately, does not work.
Does anyone know how to cancel scheduled notification?
This is how I set alarm manager for notification:
Intent myIntent = new Intent(DirctActivityfinal3.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(DirctActivityfinal3.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);
I tried this code to cancel alarm manager, but not working I still get notified
Intent intent = new Intent(ViewTicketActivity.this, DeleteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this,0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Your PendingIntents are not the same.
To cancel an alarm, the pending intent that you created to cancel the alarm with must be the same as the one you created the alarm with.
e,g in your example, You are passing different classes into each.
Intent myIntent = new Intent(DirctActivityfinal3.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(DirctActivityfinal3.this, 0, myIntent, 0);
is not the same as
Intent intent = new Intent(ViewTicketActivity.this, DeleteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this,0, intent, 0);
Complete Sample:
Intent myIntent = new Intent(myContext, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(myContext, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
then either
alarmManager.set(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), pendingIntent);
or
alarmManager.cancel(pendingIntent);
Declare Intent, PendingIntent and AlarmManager as global variables
to cancel the scheduled
pendingIntent = PendingIntent.getBroadcast(ViewTicketActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);

Categories

Resources