How to schedule my android app to do something every hour
Solution 1
take a look at the demo applications provided with android sdk
http://developer.android.com/samples/RepeatingAlarm/index.html
the look at AlarmService_Service for the implementation of the service once the alarm has been triggered
Solution 2
Just for the sake of completness, I am linking here the official docs, where the issue of the rebooting is adressed.
http://developer.android.com/training/scheduling/alarms.html#boot
Solution 3
put all of the background tasks you want to do in your app in Services
which perform tasks in the background. In the service you should be able to define a timer that causes whatever updates you want to occur every x hours.
In the onCreate()
of the widget, start the service. onCreate()
is called every time that the widget comes to life (such as when the phone starts if it is on the home screen) and will therefore guarantee that the Service
is always running.
Hope this was helpful.
Marqs
Updated on June 03, 2022Comments
-
Marqs almost 2 years
I want my app to access database every hour and read next record from the table then update desctop widget and send notification. I know that there is AlarmManager which I can use to register my Intents but they are deleted when the phone is turned off or rebooted.
Is there any other android class/service that I would update my application continuously even when I reboot my phone?
Thanks,
-
CommonsWare over 13 yearsHaving an everlasting service is a one-way ticket to a one-star rating on the Market.
-
CommonsWare over 13 yearsWith respect to "but they are deleted when the phone is turned off or rebooted", you need to implement a
BOOT_COMPLETED
receiver to re-establish your alarms after a reboot. If you are implementing an app widget, you also have the option of usingandroid:updatePeriodMillis
in your app widget metadata -- this alarm is automatically managed for you, but it is not as flexible. -
mtmurdock over 13 yearsno, i never said an "ever lasting service" was a good idea, but @marqss said he wanted a service that was always running... even after the phone was reset.
-
peterp over 11 yearsThe example does not seem to be available online any longer, and is obviously also not available with newer SDK versions. However, a similar (maybe even the same?) example can be found here: code.google.com/p/android-lab/source/browse/trunk/android-lab/…