Delete Alarm with a unique ID - android

I do have a NoteApp, where you can set reminder.
I want to implement that you can cancel the alarm.
My problem is I cant find any tutorial how to do this.
I've set an unique ID and saved it into the SQLite Database.
So when I click on Dialog "Yes" it should cancel the Alarm.
Here I set the alarm:
Intent i = new Intent(NeueNotiz.this,NoteNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(NeueNotiz.this, (int) System.currentTimeMillis(),i,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
isAlarmSet(true,String.valueOf(cal_new.getTimeInMillis()));
alarmManager.setExact(AlarmManager.RTC_WAKEUP,cal_new.getTimeInMillis(),pendingIntent);
I have absolutely no clue.
Help please

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);
See How to cancel alarm from AlarmManager link for more info.

Related

how to cancel multiple alarm using other activity

Create note Activity from where Alarm trigger! and Alarm delete code in other Activity,it delete only last trigger Alarm means if 3 Alarm are created and I want to delete Alarm 1 it deletes but when the time of Alarm 1 reaches it trigger while it already deleted and it wont trigger other Alarm 2 and 3, on static broadcast id it trigger only last alarm for example alarm 3 trigger delete code in other activity if it delete on using same pending intent in other activity how can i use the pending intent which use trigger alarm in my delete alarm activity
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
String alertTitle = mTitleText.getText().toString();
intent.putExtra(getString(R.string.alert_title), alertTitle);
// broadcastCode++;
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), broadcastCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), pendingIntent);
cv.put(mDbHelper.TIME, timeString);
cv.put(mDbHelper.DATE, dateString);
public void delete(int id)
{
db.delete( DbHelper.TABLE_NAME, DbHelper.C_ID + "="+id, null);
db.close();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), CreateNote.broadcastCode, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
The PendingIntent needs to be created exactly as it was when you started the AlarmManager.
Use PendingIntent.FLAG_UPDATE_CURRENT instead of 0 when creating the PendingIntent to cancel the alarm. FLAG_UPDATE_CURRENT is equal to the constant 134217728, not 0.
Use this code:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, _id, intent, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Hope this helps.

Canceling alarms from AlarmManager

I am working on application which is get the alarm time from database every 2 AM and then at the specified time the activity shows up, my app works fine, but when I manually change the data/time of the device, previous alarms are triggered and I should prevent to this to happens.
Here is my PendingIntent:
Intent i = new Intent(mContext, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(G.context, _id, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Which each _id is unique and when I make a receiver for date changed event before my alarm cancel method fired previous alarms go off!
Can some body help to solve this problem?
Try this code:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent);
For cancel the alarm:
Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Edit
Well you can look for the answer and modify as per your requirements:
How to stop Android AlarmManager when there is a manual system time change?
AlarmManager: how to schedule a daily alarm and deal with time changes
Intent intent1 = new Intent(G.context, DailyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context,
G.dateTimesetAlarm.getDayOfYear(), intent1, 0);
mAlarmManager.cancel(pendingIntent);
pendingIntent.cancel();

How to cancel alarm from AlarmManager

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.

How do i know if my alarm manager timer is on?

I'm using this code (from my activity) to start a periodic timer in the application:
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),(i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_SHORT).show();
I want to know how can i know (from a different activity) is the timer is running?
One possibility I can think of is to check if the pending Intent is defined.
You can call
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_NO_CREATE);
this will give you the pending Intent, or null if the Intent was not specified. This will not guarantee that the alarm was set but it is an easy way to check if the set alarm code was called before.
Another possibility is to save all set alarms in a SharedPreferences file. In this way you can look for all alarms set by your app in the shared prefs file.
As far as I can see there is no way to get a list of set alarms for your application from the AlarmManager
in stop function
Intent intent = new Intent(this, LocaitonSampleBroadcaseReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent);
just add this:
pendingIntent.cancel();
so you can use the string wrote comment Janusz
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_NO_CREATE);

How to cancel this repeating alarm?

I'm writing something like a reminder for users. Users will set reminders for their events, when the time comes, a repeating alarm will be set to trigger a status bar notification. But the alarm seems non-stop after I selected the notification or cleared the notification. I am not sure where to cancel this repeating alarm. Below are some of the codes:
Set up the repeating alarm in my main activity
alarmTime = Calendar.getInstance();
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmTime.add(Calendar.MINUTE,offset_time);
//Schedule the alarm
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 30 * 1000, sender);
In my OnReceive method, I just display the notification in status bar and set the flag as FLAG_AUTO_CANCEL
manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.medical, text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0);
notification.flags = Notification.FLAG_AUTO_CANCEL;
manager.notify(R.string.service_text, notification);
How can I stop the alarm when the user selects the notification or clears it?
Call cancel() on AlarmManager with an equivalent PendingIntent to the one you used with setRepeating():
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
I tried various methods and couldnt get it to work, so I decided to do a dirty trick. When I want to cancel my repeating alarm, I use the same method that created my alarm (and hence replacing the old one) and then cancel it right away. With this method if the boolean variable is set to true it creates an alarm ow it replaces and then cancels the replacement which has the same id:
static void CancelRepeatingAlarm(Context context, boolean creating){
//if it already exists, then replace it with this one
Intent alertIntent = new Intent(context, AlertReceiver.class);
PendingIntent timerAlarmIntent = PendingIntent
.getBroadcast(context, 100, alertIntent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (creating){
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL, timerAlarmIntent);
}else{
alarmManager.cancel(timerAlarmIntent);
}
In your MainActivity, set the alarm time. If you're going to use multiple alarms, use SharedPreferences to store their respective IDs. Here is the code:
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, _id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);
public static Context getContext() {
return mContext;
}
mContext=mainactivity.this;
In your second Activity use same ID from SharedPreferences. In my code, I get the ID from the ArrayList, Alarm_id. Last, you can use the MainActivity context here with MainActivity.getContext(). Here is the code:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentAlarm = new Intent(AlarmListviewActivity.this,
MainActivity.class);
PendingIntent morningIntent = PendingIntent.getBroadcast(MainActivity.getContext(), Alarm_id.get(positon),
intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(morningIntent);
morningIntent.cancel();

Categories

Resources