[ACCEPTED]-Android regular task (cronjob equivalent)-cron

Accepted answer
Score: 23

For Cron like tasks you have to use AlarmManager, this 3 is a system service, for using it in your 2 code you need to call:

AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE).

Full docs about AlarmManager 1 here.

Score: 5

The most suitable approach is through services. I 12 learned how to write services by looking 11 at the source code for the stock Email app 10 that is included with Android.

The general 9 idea is that you override the Service class, and 8 set up alarms to activate your service. Unlike 7 daemons and Windows services, Android services 6 aren't always running - they start up (usually 5 when activated by an alarm), perform work, then 4 shut down. In some cases, you may need to 3 acquire a partial wake lock to keep the service going until 2 it completes the task - otherwise, Android 1 may kill your service prematurely.

Score: 4

If you want to build a cronjob runner then 13 what you want is a Service:

A Service is an application 12 component that can perform long-running 11 operations in the background and does not 10 provide a user interface. Another application 9 component can start a service and it will 8 continue to run in the background even if 7 the user switches to another application. Additionally, a 6 component can bind to a service to interact 5 with it and even perform interprocess communication 4 (IPC). For example, a service might handle 3 network transactions, play music, perform 2 file I/O, or interact with a content provider, all 1 from the background.

More Related questions