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);
Related
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.
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?
I want use String start = "16:00"; in specific time, start another activity.
I must use String start = "16:00"
MainActivity.class
String start = "16:00";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarmTime(this);
}
private void setAlarmTime(Context context) {
String[] strStart = start.split(":") // delete ":"
Calendar cal_start = Calendar.getInstance();
cal_start.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strStart[0])); // hour
cal_start.set(Calendar.MINUTE, Integer.parseInt(strStart[1])); //minute
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
alarm.set(AlarmManager.RTC, cal_start.getTimeInMillis(), pIntent);
}
I want while the app is running, current time 4 o'clock , start AlarmActivity.class .
But it does not work.
How to every specific time start another activity on android?
#update
private void setAlarmTime(Context context) {
String[] strStart = start.split(":");
Calendar cal_start = Calendar.getInstance();
cal_start.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strStart[0]));
cal_start.set(Calendar.MINUTE, Integer.parseInt(strStart[1]));
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, cal_start.getTimeInMillis(), pendingIntent);
this source not work.
not work alarmManager.
Please consider to use AlarmManager.RTC_WAKEUP if you need to wake up the device even if it goes off.
alarm.set(AlarmManager.RTC_WAKEUP, cal_start.getTimeInMillis(), pIntent);
AlarmManager.RTC will NOT wake the device up.
Reference: https://developer.android.com/reference/android/app/AlarmManager.html#RTC
Btw, you do not need to pass the context reference to method:
PendingIntent pIntent = PendingIntent.getActivity(this /*can use this as it is a context already */ , 0, intent, 0);
Update:
Please also set the second and millisecond of the cal_start; otherwise it will be the values that you get the calendar instance.
cal_start.set(Calendar.SECOND, 0);
cal_start.set(Calendar.MILLISECOND, 0);
Update 2:
It works in my side, you may try to add
<uses-permission android:name="android.permission.WAKE_LOCK" />
in Manifests.
Btw, if you want this alarm repeat every day
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal_start.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
Add this permission in your manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
register your receiver class in the application tag in the manifest file
<receiver android:name=".AlarmActivity" />
Resister your alarm that will trigger AlarmActivity at a specific time in your case its 16:00
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Make sure your register class extend BroadcastReceiver like this
class AlarmActivity extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do whatever you want
// you can generate notifications here
// or can start your application activity you want
}
}
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;
}
I know this is very simple and can do anyone but i m stuck in this plz help where i did mistake
one think very important i start from one.class and stop in second.class and i can do stop after 3rd or 4th day of start.
i start alarm like this
//set Alarm method
private void SetAutoCleanningService()
{
long when= System.currentTimeMillis() + 1000*20;
long repeattime =Constant.NOTIFICATION_TimeDiff;
if(Constant.IsDebug)
System.out.println("time to trigger is ="+getDate(when, "dd/MM/yyyy hh:mm:ss.SSS"));
Intent intent = new Intent(this, AutoCleaningBroadCast.class);
intent.setAction("systweak.AutoCleanningBroadCast");
PendingIntent pendingIntent = PendingIntent.getBroadcast(ApplicationSetting.this,1234567, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, repeattime,pendingIntent);
}
And stop like this
//close Alarm method
private void StopEarlierAlarm() {
// TODO Auto-generated method stub
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AutoCleaningBroadCast.class);
PendingIntent pIntent = PendingIntent.getBroadcast(ApplicationSetting.this, 1234567, intent,0);
aManager.cancel(pIntent);
}
i use same request code but cant able to stop and on every alarm i show one notification that will be repeat in every alarm calling
You can cancel the alarm like this:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1352, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Also, you should remove Intent.FILL_IN_DATA from your call to getBroadcast() in the code where you set the alarm.
Cancel your PendingIntent too.
pIntent.cancel();
Make sure context used in creating intent is same.
If not possible to use same Context, then create intent as a global Static variable and use later.
Check this, it may help you
Step 1:initialize the alarm manager.
AlarmManager alarmManager1;
Step 2:
// for Notification Process
Intent myIntent1 = new Intent(Settings.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(SettingsPage.this, 2, myIntent1, 2);
alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1Notify = Calendar.getInstance();
calendar1Notify.setTimeInMillis(System.currentTimeMillis());
calendar1Notify.set(Calendar.HOUR_OF_DAY, 8);
calendar1Notify.set(Calendar.MINUTE, 15);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), pendingintent2);
long time24h = 24 * 60 * 60 * 1000;
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), time24h,pendingintent2);
Step 3-Stop alarm manager:
alarmManager1.cancel(pendingintent2);