Repeating background tasks in Android

323 views Asked by At

I have to write an application that executes a task at a specific interval. A task consists of recording some data, collected from GPS and other sensors, and occasionally making SQLite transactions.

The device (a smartwatch with 4.4.2 API level 19, so without wearable support) is asleep for most of the time, in which case I want it to wake up and stay awake until the task is completed.

What is the correct and best way to do this in Android, that is also relatively easy to test?

Thank you very much.

1

There are 1 answers

1
Helmi On

You need to use AlarmManager for cron tasks, this is a system service that can run at specified intervals, example of call:

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

Full docs about AlarmManager here.