AlarmManager Dilemma for Android - android

I want to set a repeating alarm monthly; but my app resets the alarm everytime it boots up. So is it bad practice to do it this way rather than setting a repeating alarm?
(The Alarm is set for a specific day of the month. (e.g. The 8th, 16th, 21st, etc). So if it is past that day, it simply +1's on the total the current month.)
I also run a service on boot up to set any alarms so even if they don't go into the app for a month, it will be reset again.
Again, is this the best way of doing this or is this bad practice?

So is it bad practice to do it this way rather than setting a repeating alarm?
If it works for you, it is probably fine.
I also run a service on boot up to set any alarms so even if they don't go into the app for a month, it will be reset again.
That is a little odd. Getting control at boot time to re-establish alarms is fine, but you should not need a service for that.

Related

Do you need to remove an alarm before setting new one

I have an Android alarmManager being set and a broadcastReceiver to pick it up, whenever the app triggers onResume, I am forcing a new alarm to be set.
So I'm wondering for best practices and considering good resource management, should I be removing the first alarm (if it hasn't triggered) before I reset a new alarm ?
Potentially if an alarm doesn't trigger, each time the user re-opens the app, is it creating more resource usage even if the alarm is for the same time ?
Yes you should cancel it when it is appropriate.
I suspect you will have a lot of wasted cycles otherwise. For Example,
App opens at Time T.
Schedule an alarm for T'.
Next the user opens the app again at some time, X seconds prior to T'.
You will then schedule an alarm presumable at another time T''.
However you will get an alarm firing in X secs anyways. If you ignore it, you are simply wasting battery if the phone had to wake in order to deliver the alarm.
If you create the same PendingIntent for your alarm, then the previous one will automatically be canceled when you set it again.
See AlarmManager.set(), PendingIntent.

Count-down timer/alarm API for android

I'm learning Android and want to write some kind of count-down timer application, so I get a ring-tone after certain minutes are elapsed. The timer also should work if the user has closed the application or switched to another one. If my application is running, it should be able to display the remaining time.
I've tried with CountDownTimer, but this seems only to work when the phone is activated, but not like the alarms which could ring you up at the morning. What other similar API alternatives are there to activate the device if the time is elapsed?
You can use AlarmManager for this purpose.
count-down and alarm are two very different things (even thou both count time).
Count-down you probably want to run a service and put a notification with the flag ongoing = true updating the value of the time.
Alarm you want to use the AlarmManager (as pointed out by #PgmFreek) that you can schedule a specific time that the system will call an Intent for you.

Android - Start an IntentService on a specific day/time each week

I'm working on an app that has an IntentService that shows a Notification with showtimes for a local venue. I want the IntentService to be called every Fri at 5am or at boot on Friday morning.
What would be the best way to do this?
Right now I'm thinking of having a parent IntentService that will start at each boot and check what day it is and run the child service accordingly, but I don't know how to deal with a scenario where the user doesn't reboot.
Also, how would I run just the IntentService at boot and not the entire GUI?
Thank you
You can use the AlarmManager and use the setRepeating() method to set an Alarm that goes off every week at the same time.
Your booting requirement is probably not worth the effort, as very very few people reboot their mobile devices often. I've personally gone months without turning my phone off.
However, you don't need a parent service. You can do the checking of the day when the child service first starts up, and have it kill itself if its not Friday.

Android, set custom timing of AlarmManager... Please advise

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

about Timing execution

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.

Categories

Resources