Android Alarm Clock - Remove alarms programmatically - android

I am writing an application in which user can configure alerts/alarms. At this point, I have everything working expect the one. I am firing off an alarm using
Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, "Some message!");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, calendar.get(Calendar.HOUR_OF_DAY));
alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, calendar.get(Calendar.MINUTE));
alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
context.startActivity(alarmIntent);
I want to remove this alarm once user has dismissed using the Dismiss button. I can see the alarms being still there in the alarm clock which I set using above code through my application.
Is there some way to get a cursor or something similar on the alarms being there in the alarm clock? This will help me iterate over them and remove the ones I want.
Any help would be appreciated, Thanks in advance.

As it was answered here: there is no supported API to this.
Official documentation says that
If a time of day is specified, and EXTRA_SKIP_UI is true, and the
alarm is not repeating, the implementation should remove this alarm
after it has been dismissed.
But different manufactures implement their own alarm clocks and I'm not sure if someone supporrts this.
I have tried it on JB emulator and looks like it dosen't work. Maybe, on KitKat?
Only this one is ok on JB:
If an identical alarm exists matching all parameters, the implementation
may re-use it instead of creating a new one
So, maybe, better solution is to use youre own alarm, created with AlarmManager or warn user that he has to remove Alarms by hands (to make it more obvious - not use AlarmClock.EXTRA_SKIP_UI).

call method cancel(...) from AlarmManager, using the same PendingIntent you used to set the alarm. Example:
mAlarmPendingIntent = PendingIntent.getActivity(this, requestCode, intent, flags);
this.getAlarmManager().cancel(mAlarmPendingIntent);
this refers to the Activity or the Service from which you are cancelling the alarm

Related

Developing reminder application in Android

I'm creating a reminder application that will show notifications on scheduled time, I'll be storing reminder related data in a database and created the working database and content provider for it, initially I thought of creating an alarm whenever user creates a new reminder, but now I'm not able to fire the notification whenever the alarm triggers, and don't know how can I retrieve the reminder related info from the database, and for repeating reminders like daily or weekly I want to reschedule the same alarm after it triggers as the android developer docs suggested so, but don't have the exact idea of it. Please guide me through this which approach will be better and is there any other better way to do it, and any working demo will be very helpful.
This seems like it is quite a few questions bundled up into one. I would suggest searching for each individual piece as all of the answers to your questions are on this site.
In any case, I copied over an answer on how to set a repeating alarm
AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(CurrentActivity.this, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);
Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);
Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
With this code an intent will be fired every day that calls Alarm.class. It is up to you to figure out if you need a service, intent, or anything else as Alarm.class, as it is unclear what the alarm needs to do based on your description.

Deleteing alarms in alarm manager

For example, i have a calendar, which allows user to set every day alarm. For this function i took alarm manager's method setRepeating(). But my problem comes when user wants to delete alarm from specific day, but not deleting all alarms. How can i make that? Is alarm manager the only way to set alarms? Any ideas will be appreciated.
Cleverest thing i came up - to set broadcast receiver which will trigger every day at 00:00 and set all alarms by method alarmManager.set(). Deleted alarms will be at database and if today alarm will match to deleted one - broadcast receiver will not set this alarm.
AlarmManager has a method cancel(); and it may help you. But in your case I would have been more confident with going the approach you given as a hind in your question.
you can use .cancel() method of Alarm manager..But remember one thing.You have to give the same id in the pending intent that you supply to the alarm manager to that you had set the alarm with.
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(),id,intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
the id supplied should be the same that was used to set the alarm.
I hope that will help you..
I would suggest you to save the id for each alarms somewhere and use it to cancel it if you need to.

Android - Alarms sometimes fires at the wrong time using AlarmManager

This problem seems a little bit odd, but if someone as encounter something like this, please help me...
I created an Alarm Scheduler, that sends an alarm to the user using AlarmManager, through this code:
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("tk_alert_id", lastAlertId.getId()+"");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
The problem is that, sometimes, I receive this alarm on my AlarmReceiver (BroadcastReceiver) at a wrong time, as you can see in the image bellow:
image, and I can't figure out what's the problem... I checked the time for date and was set as "2015-05-27 17:00:00", but it was received a little minutes earlier (around 16:57) ...
Does anyone knows what kind of problem I am encountering here?
For API levels <19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time.
Api levels >=19 and above this no longer works. There was change in android so that all repeating alarms are inexact.
So if you would like to achieve exact repeating alarm use AlarmManager.setExact().
See this question for more info.
Edit
For your purpose (a one-off alarm, at a precise time) use alarmManager.setExact(....). See docs

Android Dev: AlarmManager Reschedule and Service

please refer to
this for the issue i was facing....as suggested by the person who replied to my problem + what my additional research showed is that i need to use AlarmManager to solve my sleep & timertask issue...since, i have started changing the existing code to utilize PendingIntent along with AlarmManager with: Thread thr = new Thread(null, mTask, "ServiceName"); in the service...
after reading the AlarmManager documentation several times, i dont know how to reschedule a AlarmManager... im working on a Profile Switcher application, which will various run intervals (like i described in my problem thread)...
Does anyone have any suggestion?
i dont know how to reschedule a AlarmManager
Use cancel() to remove an existing alarm, and use set() or any of the other set...() methods to schedule alarms. To "reschedule" alarms, you cancel the old and create the new.
If you use the same intentString and the same request code, your alarms will be overwritten.
In your intent, you can use the following flag:
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notification.getId(), new Intent(intentString), PendingIntent.FLAG_UPDATE_CURRENT);
long startDateTimeMillis = notification.getDateTime().toDateTime().getMillis();
MyApplication.alarmManager().setExact(AlarmManager.RTC_WAKEUP,
startDateTimeMillis,
pendingIntent);
Related to the alarm itself, you can use the methods setExact() or set() and you can search about them to check which one fits you better.

How to set time of choice in a Calendar object?

Sorry if the question is a bit misleading.
I asked another question earlier and by its help I got this far. Here is the problem.
I'm supposed to launch a Service at a particular time. Now... the service starts if I launch it instantly by using current time from a Calendar object, but when I set the time manually, the service doesn't launch. Here's the code:
Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.HOUR_OF_DAY, 21);
myCal.set(Calendar.MINUTE, 4);
// Will use putExtras here to give service the details of which profile to activate
Intent intent = new Intent(Create.this, AutoBot.class);
PendingIntent pintent = PendingIntent.getService(Create.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, myCal.getTimeInMillis(), 5*1000, pintent);
Now this should technically work, but its not. And that is why I need your help :(
Also, If I have multiple triggers (alarms per se), what would the third argument of setRepeating be?
I think you need to set up day month and year as well. Date in millis is from 1 Jan 1970
I think this link may answer your question. It explains a there is a limitation in
the system where the wake lock the alarm manager holds will be released too early when
delivering to a service. See this discussion: http://groups.google.com/group/android-developers/browse_thread/thread/2b98206d18a0192f/
Hope this helps.

Categories

Resources