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.
Related
I am creating a notification app, which will alert user after they set a reminder notification. My current implementation logic is:
Create a Service, which starts running in background when user opens their app.
In onCreate() method of service, I am implementing a Timer task which will repeat after 5000ms interval and will call a method, which will check all reminders set by user and notify user if any reminder is set for current time.
I use broadcast receiver to run the service on Boot_Completed event, if in case user turns off their phone.
This implementation is working good, I have faced no issues with it, but my concern is that is this a good practice? Keeping in mind that service running and checking every 5 secs will consume battery. Also if user turns on Stamina Mode, Power saving mode or any such mode, will OS kill my service. Is there anything I can do to give priority to my Service not to be killed.
If there is any other more efficient way to implement this sort of task, I want to implement that in my project.
Looking forward for suggestions.
Much Appreciated.
best approach is wakeful intent service with alarm receiver as mentioned here
https://github.com/commonsguy/cwac-wakeful
all good but use AlarmManager.setRepeating() as timer. the intent come even if your app killed.
I need to keep service running to monitor changes through content observer.
I also need to know , Can my alarms set for periodic intervals can be erased ?
If yes then When ?
Can task killers erase Alarms for version onwards 2.2 ?
If you want to keep a service running, use Service.startForeground. Then the Android system won't kill it, except under the most extreme circumstances.
You need to supply a notification which is continuously shown to the user so that they know they have a service running which is potentially using resources. This seems to be a deliberate (and sensible) design decision.
So long as you do that, and so long as your ContentObserver actually works, there should be no need at all for you to wake up your activity or service code using AlarmManager. Your service should just continuously run and your code should be called whenever the content changes.
I'm new to Android so I want to make sure that the following solution is the correct one.
The problem:
I want to sync the device's local database with a database on my server, via a webservice, every 10 minutes. I already have a web service call that I can make that returns the new/updated records. What I'm wondering is what is the best way to schedule this task. I want the databases to sync even when the application is not running.
My solution (is this the correct route to go?):
I will have one BroadcastReceiver that listens for android.intent.action.BOOT_COMPLETED, in it's onReceive I will create an AlarmManager that sends a message to MyReceiver (via a PendingIntent) every 10 minutes. Also, in my application's startup I will do the same (create an alarm to send messages to the MyReceiver via a PendingIntent) - Since both alarms are sending messages to MyReceiver and their corresponding PendingIntents are initialized with PendingIntent.FLAG_UPDATE_CURRENT will the new alarm override the old one? (this is what I want to do, in case for some reason the alarm gets cancelled after device boot it should be restarted when the application starts).
In MyReceiver's onReceive() I will create a MyIntentService (this instance will make the webservice call and update the local database).
Is this a good solution? Any suggestions?
Thanks
Solution is fine...Actually all the AlarmManager instances get cleared when device turned off and rebooted.
The simple way is that...
First create AlarmManager when application started.
Second in onReceive of BOOT_COMPLETED BroadcastReceiver.
Its enough, PendingIntent.FLAG_UPDATE_CURRENT will make sure of having only one activated alarm at a time.
In this way, alarm registered when your application started. There will be no issue if its already registered via BOOT_COMPLETED. Activated alarm will deactivated when you turn off your device, but BroadcastReceiver to BOOT_COMPLETED will take care of registration new alarm at next boot.
If you decide that this answers your question, please mark it as "accepted". This will raise both your and my reputation score.
Also you need to review your interval to use network, it might be very resource consuming for device and user. One policy might be to have longer period of interval and check for update when user starts your app (this might not be user friendly but can save many system resources and battery power as well). Try to find some better policy according to your needs.
Using FLAG_UPDATE_CURRENT in that manner will override the existing PendingIntent if one exists. I'm not positive but I believe that as soon as you get into onReceive, the PendingIntent is consumed so it's no longer there to be overridden. In either case, it sounds like this is the functionality you are looking for and yes it's a good way to solve this kind of problem. My only other suggestion would be if the 10 minute interval timing is not absolutely critical then use one of the INTERVAL_ schedules (INTERVAL_FIFTEEN_MINUTES for example) in your AlarmManager to help conserve battery life; basically it lets allows all apps that run on intervals to "batch" their work together and wake the device up less frequently.
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.
I have a service that will monitor location changes daily. What I know so far that to start a service at boot, I have to follow the linked tutorial. This way I can get the service started at boot, but to save battery I need it only between 9am-9pm.
Question is pretty simple, so I will repeat:
How can I ensure a service is started at 9am and stopped 9pm every day?
Use AlarmManager to set two alarms, each with a PendingIntent that will call startService() on your service, but with distinct action strings ('start', 'stop'). When onStart() of your service detects the 'stop' action Intent, it arranges for an orderly shutdown (e.g., stopSelf()).
This will fail if the user applies a task manager to you in Android 2.1 or earlier, since the technique they tend to use will wipe out your alarms (in addition to killing the service). In that case, the user is presumably voting for your service to not run, so you should try to accommodate the user's wishes.
CommonsWare is right. This is the best way.
You are writing an application and you had better not to change anything out of the application. If you want to add a system service(on boot started service), you need to modify the BSP and add your service to systemserver.java. It's not recommended.CommonsWare's suggestion can do this work.
As you said about the activity, you can start the activicy when you receive the boot broadcast. Then do what your want.....