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.
Related
I have an application that will create a persistent notification but I need to update this notification each day so is there a way to create a time listener or how to do it otherwise but I need this update to be performed even if the activity is not started, so even if the user did not open the application, I saw this page but it's just for an app that is still in its started state once it's in its pause state... it won't work anymore... any idea?
The Best way is to use AlarmManager see this link http://www.techrepublic.com/blog/android-app-builder/use-androids-alarmmanager-to-schedule-an-event/
Otherwise you will need to write service to keep track on time.
Good luck
You could use an AlarmManager.
http://developer.android.com/reference/android/app/AlarmManager.html
Set up the alarm to trigger at the time you want to handle your event.
When you complete your event code, set up a new Alarm before you exit.
Note that alarms do not survive a reboot so you would need to set them up again at device boot (if required)
I want my application to trigger an event at a given time. At the moment I'm using AlarmManager. But this will be lost if the users phone is restarted, or the user uses a task killer or ends the application with the android task manager.
So what is the best way to do this. Should I just use an alarm and have it repeatedly set in a service so when it is deleted it come right back?
You can use awake alarm service on boot completed so that all the task will be re scheduled after reboot. You can store alarm id and details in database.
Might be overkill for what you need, but check out CommonWare's WakefulIntentService:
https://github.com/commonsguy/cwac-wakeful
Even if you don't want to fire up an entire IntentService when your alarm is triggered, his code should give you some idea of how you can persist your alarms across device reboots.
Hi I need to set AlarmManager to run a reminder for me to take medication. I need to repeat it by custom amount of days and custom amount of times to take in the day.
So is there an efficient way to set the AlarmManager or CommonsWare's Implementation of the AlarmManager to remind me "twice a day starting at 9AM for the next 5 days" to remind me to take medication? Pls advice and tnx in advance for any constructive help in sample code and in relevant tutorials.
I haven't looked into Mark's AlarmManager implementation, but there is no way, in general, to get the bare AlarmManager to do what you are trying to do. You can schedule a single alarm, at a specific time, or a repeating alarm, that repeats at fixed intervals. If you want something that handles complex schedules like the one you describe, you'll have to write or find code that does it.
You want to use a PendingIntent with the AlarmManager. The idea is to schedule the pendingIntent with the alarmManager, have that trigger an intentService or broadcast, setup another pendingIntent with the alarmManager for the next desired event. You want to keep in mind that you'll need the BOOT_RECEIVED permission in case the user reboots their device. I have complex scheduling in Audio Control and this is exactly what I do.
Here is a pretty decent tutorial of what I mean:
http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
You need to schedule an alarm to the next time you want to take the medicine - according to your algorithm (for example if its twice a day, and you got to the pending intent callback for the first time today, then schedule the next alarm to start after [6,7,8,9,10...] hours).
You will need to save both last time of the alarm launch and the user settings in shared prefs/file/DB.
You need to handle process down (android killed it or the device was rebooted). In the case of device reboot you should use the boot receiver to start your service, but you need to remember that from android 3.1 the user has to use at least one time the GUI in order for you to intercept the boot completed receiver. The boot completed receiver should look when was the last time that the alarm launched, and according to the user settings set the next alarm launch.
In the case of android killed your service, you will need to make research, i can't help here.
see example
Is there a way to send a notification to the user that app has been downloaded but a certain task from the app is not yet complete even after certain period - say a month. One way is a background service which should come alive every month in this case, check the app state (in sharedprefs) and then send a notification. Is there some other easier way in Android without writing custom service.
Here's how I would do it. Schedule an alarm using the AlarmManager to go off a month from today. That alarm can trigger some code inside of a Receiver or otherwise to check whether the said event has occured. If it hasn't, you can then show a Dialog or whatever.
In order to wake up your app after some amount of time (in your example a month) you're going to have to set an alarm. You can use AlarmManager for that. If all you're going to do is check SharedPreferences, you can do that in a broadcast receiver. You can send your notification there.
i have a task: there are some data,but i want to delete it,so provides the user with options as 3 days ,5days ...to delete it. i want to use alarmmanager,but when the user close the device or modify the system time,the Timer will no longer accurately,how to avoid it .thank you
If you use AlarmManager with an RTC alarm, the alarm will adjust for changes to the system time. The possible exception would be if the time change skips over the time of the alarm -- I have not tried that.
For reboots, you will need to reschedule your alarms. This is typically accomplished via a BOOT_COMPLETED BroadcastReceiver.