Not able to cancel Pending Intent using Alarm manager - android

This is the code to set u and delete the notifications. please let me know if you need more details. The only solutions on stack overflow are about the pending intent to be same. I already tried that solution and it didn't work.
public void setAlarm () throws java.text.ParseException {
RealmResults<Reminder> realmResults = getAllLatest();
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Reminder reminder = getLatestReminder(realmResults);
int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
PendingIntent pendingIntent = getPendingIntentFromReminder(reminder);
Log.v("Reminder is: ", String.valueOf(reminder));
Calendar calendar = Calendar.getInstance();
//calendar.setTimeZone(TimeZone.getTimeZone());
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);
}
private PendingIntent getPendingIntentFromReminder(Reminder reminder) throws ParseException {
int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
Intent intent = new Intent(this, MyReceiver.class);
intent.setAction(reminder.getName());
Uri data = Uri.withAppendedPath(Uri.parse("TYS"),
String.valueOf(reminder.getId()));
intent.setData(data);
Calendar calendar = Calendar.getInstance();
//calendar.setTimeZone(TimeZone.getTimeZone());
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
//Log.v("Test", calendar.getTimeZone().toString());
// Log.v("Hours to show", String.valueOf(hour));
// Log.v("Minutes to show", String.valueOf(minutes));
intent.putExtra("reminderTitle", reminder.getName());
// Log.v("Reminder Name", reminder.getName());
intent.putExtra("notificationType", 1);
intent.putExtra("reminderId", String.valueOf(reminder.getId()));
intent.putExtra("reminderSubtitle", reminder.getSubTitle());
intent.setAction(Long.toString(System.currentTimeMillis()));
Log.v("set Alarm", String.valueOf(intent));
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
return alarmIntent;
}
public void deleteReminderIntent(Reminder reminder) throws ParseException {
Log.v("deleteReminderIntent", String.valueOf(reminder));
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent =getPendingIntentFromReminder(reminder);
alarmMgr.cancel(pendingIntent);
pendingIntent.cancel();
//alarmMgr.cancel(temp);
// alarmMgr.cancel(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}

In getPendingIntentFromReminder() you set the ACTION in the Intent to the current time. When you call the method to get a PendingIntent to cancel your alarm, the current time is different, so the ACTION will not match the PendingIntent you have used to set the alarm.
In order to cancel the alarm, you need to use an Intent that will match the Intent that you used to set the alarm. The following parameters in the Intent must match:
ACTION
CATEGORY
DATA
COMPONENT
Any "extras" in the Intent are ignored, so they do not need to match.
For more details see https://stackoverflow.com/a/29590084/769265 and
Is it possible to create multiple PendingIntents with the same requestCode and different extras?

Related

How to cancel the alarm manager daily notifications?

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.

Alarm Manager triggering only the last scheduled task in Android

I am using Alarm Manager to schedule different jobs to be executed at different hours of the day. When i am scheduling only one job it is working expectedly, but when i schedule multiple jobs, only the last scheduled gets executed. Below are my codes.
private void triggerAlarm(int hr, int min, String sid, String cid) {
Intent intentToFire = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
intentToFire.putExtra("cid", cid);
intentToFire.putExtra("sid", sid);
intentToFire.setAction(ACTION_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intentToFire, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().
getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hr);
c.set(Calendar.MINUTE, min);
c.set(Calendar.SECOND, 0);
long afterTwoMinutes = c.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, afterTwoMinutes, alarmIntent);
//sendBroadcast(intentToFire);
}
You can use id for your AlarmReceiver I Also Using this and hope it's also works for you
in targetCal Param just pass your calander obje where you selecting time for alarm
like this
private void setAlarm(Calendar targetCal) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
final int _id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);
// PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
appIntent);
}
Hope it works for you.

Android cancelling pending intent not working

I am facing this really weird problem. I have made a working alarm system. But when I delete the alarm, it calls it.
AlarmBuddy.java (Working class to set original alarm)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DAY_OF_MONTH, d);
calendar.set(Calendar.HOUR, 8);
calendar.set(Calendar.MINUTE, repeat%60);
final Intent myIntent = new Intent(this.context, AlarmMiddle.class);
myIntent.putExtra("name", name);
myIntent.putExtra("id", repeat);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
super.onBackPressed();
PendingIntent pending_intent = PendingIntent.getBroadcast(AlarmBuddy.this, repeat, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
ListRecord.java THE FLAW
public Boolean cancelAlarm(int id){
Intent myIntent = new Intent();
PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT).cancel();
return false;
}
This function is to cancel the alarm. This has some problem to say this again:
The code can SET AND CALL an alarm. But when I delete it from a DIFFERENT class, it immediately calls the alarm. Maybe I am just a noob. Or maybe this is an unsolvable problem.
This is working for me at all in my application see link
Intent intent = new Intent(getBaseContext(), Your_Service.class);
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), reqcode, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
To cancel the alarm, you need to use the same PendingIntent as you used to set it. You should also tell the AlarmManager to cancel the alarm as well as canceling the PendingIntent. Try this:
public Boolean cancelAlarm(int id) {
Intent myIntent = new Intent(context, AlarmMiddle.class);
PendingIntent pending_intent = PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_NO_CREATE);
if (pending_intent != null) {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pending_intent);
pending_intent.cancel();
}
return false;
}

Use set() in Alarm Manager with values i have

How do i use the two vales i have (hour and minute ) to set and alarm at that time? The values i have unfortuntly arent formated so if hour is > 12 it wont go to one (Its in milatary time format) .Did i set up the pending intent correctly?
public void setUpAlarm(int hour,int minute) {
long newTime;
Intent alarm = Intent(setAlarmList.this, AlarmReciever.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent waiting = PendingIntent.getBroadcast(setAlarmList.this, 0, alarm, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, hour.);
use below code
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, h); // h->hour in 24 hrs system
cal.set(Calendar.MINUTE, m); // m->minutes
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
if (android.os.Build.VERSION.SDK_INT>16)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
}else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
} //alarmIntent is your PendingIntent
read this documentation

Alarm not getting cancelled

I am setting the alarm in one activity, and want to cancel it in another class which is a service. Following is my code:
Intent intent = new Intent(SetAlarm.this, AlertFriend.class);
PendingIntent pi = PendingIntent.getActivity(SetAlarm.this, unique_id, inent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, hour);
c1.set(Calendar.MINUTE, min);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pi);
And following is where i am deleting the alarm (where a is pi):
Intent intent = new Intent(context, AlertFriend.class);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(PendingIntent.getActivity(context, a, intent,PendingIntent.FLAG_UPDATE_CURRENT));

Categories

Resources