In my app I use alarm manager to daily show notifications, but I want to create an option for users to cancel the daily notifications.On user click the working alarm manager has to permanently stop.
How to achieve this?
This is the method that is being called on app installation and then notification starts appearing daily, how to cancel it permanently?
public void AlaramNotification() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(StartActivity.this, AlarmReceiver.class);
PendingIntent broadcast = PendingIntent.getBroadcast(StartActivity.this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
cal = Calendar.getInstance();
Calendar now = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 15);
cal.add(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM,Calendar.AM);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (now.after(cal)) {
Log.d("Hey","Added a day");
cal.add(Calendar.DATE, 1);
}
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, broadcast);
}
else {
}
}
You can try this way:
AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(StartActivity.this, AlarmReceiver.class);
PendingIntent broadcast = PendingIntent.getActivity(StartActivity.this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(broadcast);
When user cancels daily notification use cancel() on alarmManager with pendingintent as shown below to stop alarmManager.
alarmManager.cancel(broadcast);
You may need to make broadcast a global variable.
Related
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
In my app, I need to start a service at 2:00pm daily. Right now I wrote the code to trigger the alarm once, this code is ran every time I open the app:
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DownloadReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.cancel(pIntent);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY,refreshhour);
cal.set(Calendar.MINUTE,refreshmin);
cal.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > cal.getTimeInMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
if(android.os.Build.VERSION.SDK_INT>=23) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pIntent);
}
else{
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pIntent);
}
Q1. I used setAndAllowWhileIdle() for sdk above 23 in case the device is in Doze mode. I cannot find any option in this function that I can set the alarm to repeat every day.
Q2. I also have questions about setInexactRepeating() , normally it is set to repeat every day by setting the parameter INTERVAL_DAY , but in the docs, it says
As of API 19, all repeating alarms will be inexact and subject to
batching with other alarms regardless of their stated repeat interval.
Does this mean INTERVAL_DAY does not work anymore, so how can I set the alarm daily without rerunning this function and reset alarmManager?
Try below code will solve your problem-
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
boolean flag = (PendingIntent.getBroadcast(this, 0,
new Intent("totime.action.string"),
PendingIntent.FLAG_NO_CREATE) != null);
if(!flag)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent("totime.action.string");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) Data_Graph.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),(24*60*60*1000), pendingIntent);
}
I want to display a notification from my application at a specific time say 6.30 am daily. I have successfully done that. Following is the code which executes at specified time. However the code works only if the app is in open state or put to background. It does not work if I reboot the device and don't launch the app, also does not work if I kill the app .
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Following is the receiver BroadCast method
#Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
The alarm service class displays a notification.
/...Display notification.../
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);
I have a repeating alarm set, my problem is that after cancelling it doesn't cancel (I'm checking this with Log.v()
This is how I create the alarm (in an IntentService)
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentToFire = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
private void rescheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, TIME_TO_REFRESH);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CHECK_TIME, alarmIntent);
}
Then in an Activity I have a button on upon it's click it calls this code
private OnClickListener btnCloseApplicationListener = new OnClickListener() {
public void onClick(View v) {
intentToCancel = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
alarmIntent = PendingIntent.getBroadcast(v.getContext(), 0, intentToCancel, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(alarmIntent);
finish();
}
};
After the finish() is executed I keep seeing the logs I have in DDMS window.
How can I cancel it?
Thanks in advance!
try using same context for the intent.
The intents have to match and I think your problem is that you are using different context when trying to cancel the alarm.
alarmIntent = PendingIntent.getBroadcast(this, 0, intentToCancel, 0);