I'm making an application in which I have implemented a reminder using AlarmManager. Now, if the user wants to cancel this alarm, how can I do the same?
Also, once the application has asked for alarm services(using alarm manager) and set an alarm, then can we get the details of the alarm we set?
http://developer.android.com/reference/android/app/AlarmManager.html
Yes, you can cancel the alarm. Make sure you're using the same PendingIntent you used when creating the alarm.
What details are you looking for? Perhaps its best to store these using SharedPreferences.
Related
I made a simple application according this manual
But I don't want to receive alarm intent all the time, so I add switch for cancelling alarm. When I switch it on alarm starts, when I switch it off alarm cancels. But sometimes I close my app and after restart it doesn't know whether alarm is set or not for check or uncheck this switch.
What is best practices to determine if alarm was set on my application?
Unfortunately there is no publicly documented way to get scheduled alarms from AlarmManager. If you need this info you have to keep track of them yourself.
I have a broadcast receiver for receiving the 'BOOT_COMPLETED' event. What I want to do in the OnReceive method is to re-schedule an alarm whose time is pre-defined by the user. How would I go about getting the alarm time that the user entered into the application? I tried looking at SharedPreferences but these don't seem to be accessible out-with Activity classes. Does anyone have any ideas on how I could go about getting this information?
You don't have to set the alarm at boot time. You can use AlarmManager to handle that for you. You can even set repeating alarms with it.
folks! I have Service, which checks in onStartCommand() whether auto update was set in user preferences and sets AlarmManager update time if needed. So, I want to acomplish following: consider that AlarmManager is alread set, and user turns auto update off, I want to cancel the alarm. The only idea I have is to broadcast custom intent to service that preferences were changed. Is there another way to do it?
UPD or I just need to call stopService()?
A broadcast is definitely the best way to communicate between an activity/widget and a service.
My application shows content for a site that also has a notification system. I want to show if there are new notifications, and I am using an AlarmManager that calls an IntentService.
My question is: where should I start/register this AlarmManager? I've put it in the onCreate() of my activity just for proof-of-concept (and its working fine, thank you very much :) ), but if you would start that activity twice, you would get multiple alarms.
The only possible solution I've come up with is this, but I don't know if this would be best practice
Start the manager in an onCreate() if the preference "alarm started" is false
Set some variable that it is started in preferences.
Now if the alarm stops for some reason, there's no way to restart it. So, a variation would be:
Always call cancel in the onCreate()
And then always set the Alarm.
This seems like a common pattern: Wanting to periodically get information with an alarm, and not setting that alarm more then once. How should I do this? When, where and how to register the alarm?
Also, continueing on #Zelimir 's comment: can you check if a certain alarm is allready set?
Ideally, the alarm would be set regardless of the activity being started or not of course, but that might be another thing.
For completeness, this is the code I'm currently using to start the alarm:
AlarmManager alMan = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, CommentService.class);
PendingIntent penInt = PendingIntent.getService(this, 0, i, 0);
alMan.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
penInt);
For even more completeness, the app description / situation.
The app is basically showing blogs (journals if you will) from a certain page. It has activities for adding entry, viewing entries, adding comments, etc. On the 'mother' site there is an option to recieve notifications (like the number you see here on SO too when you get a message). I want to show if there are new messages, and so retrieve them every xx minutes. It would be shown in the notificationbar for now, but it might feed some sort of widget later.
If you need more info: the app is called Androblip and it supports a site called blipfoto.com
When, where and how to register the alarm?
That is impossible to answer in the abstract. It depends entirely upon what the business rules are for your app, which you declined to supply in your question.
If the monitoring is to be happening all the time, a typical pattern is to register the alarm:
in onCreate() of your main activity for the very first run of your app
in a BOOT_COMPLETED BroadcastReceiver, to handle reboots, which wipe the AlarmManager roster
can you check if a certain alarm is allready set?
No, but you can cancel it without issue. Just create an equivalent PendingIntent and call cancel() on the AlarmManager.
I am setting an alarm for my reminder app:
PendingIntent sender = PendingIntent.getBroadcast(AddToDoList.this,
Integer.parseInt(DBHelperClass.getMaxPrimaryId()), intent, 0);
i am using a unique value (primary ID) to set an alarm. When i want to edit the alarm i am using the same key. But what i see is that both the alarms go off! original as well as edited one.
how do i cancel the old alarm ?
Also these alarms are not working once i switchOff & then again switchOn my device .
how do i cancel the old alarm ?
Call cancel() with your original PendingIntent.
Also these alarms are not working once
i switchOff & then again switchOn my
device .
Alarms do not persist after a reboot. You will need to reschedule the alarms, possibly by a BroadcastReceiver that gets control at boot time.
You should consider changing the way you make Intents unique - requestCode is not officially defined. Try keeping Intent's data different for example.
About persisting the alarm trough reboots, I'm not sure but maybe RTC_WAKEUP flag is the closest you can get.