-2

I'm thinking of writing a simple Android which performs a task (e.g retrieve current location or send some values to DB) with these requirements:

  • The task is executed on certain interval, say per 1 minute or 5 minutes
  • Every time the task starts, it should run even though the app is minimized.
  • The task can be cancelable/resumable at any time (by the user, clicking a button in the app).

What's the proper way to implement this. IntentService? JobIntentService? JobScheduler?

pfmaggi
  • 6,116
  • 22
  • 43
anta40
  • 6,511
  • 7
  • 46
  • 73

2 Answers2

0

Based on your necessities there are three ways:

In all three cases you need to store a reference to your job (the first two choices let you store and id).

Of course you can also use a service for doing such operations, but in all the cases be aware of Doze Mode and it's side effects on your app.

Hope this will give you some hints. Cheers.

TomD88
  • 721
  • 6
  • 11
  • This is the first time I heard of PeriodicWorkRequest. Seems to fit my requirements. Let me give it a try. – anta40 Apr 26 '19 at 10:22
0

Given your requirements, and especially the 1 or 5 minutes repetition, the only option is to use a foreground service.

Keep in mind that the device will try to enter Doze mode after some inactivity time. You can force the device to stay awake but this is really bad for the battery.

Starting from Android Oreo, getting location while your app is in the background has some limitation to preserve the battery: https://developer.android.com/about/versions/oreo/background-location-limits

pfmaggi
  • 6,116
  • 22
  • 43