0

I am working on application which tracking user way to work and back to home. Application is based on widget (where are printing informations to user) and background service which checking localizations and cunting informations and displaying on widget.

Before Android 9 I used AlarmManager and background service to every couple of minutes checking user localization and printing informtions on widget. In Android 9 there are Service limitations and I cannot repeat service no less then 15 minutes. 15 minutes is too much, some users can have 5 minutes in road to work or home.

I tried JobService, JobIntentService, AlarmManager and no one is working less then 15 minutes. Only Foreground Service is working but I cannot use it because user are not running activity, all is working in background using widget.

Has anybody have a idea is that possible to run service in background repeating in less then 15 minutes. Or maybe any other idea how to achieve my requirements? I need every couple o minutes (1-5 minutes) checking user localizations and counting, then sending datas to widget view.

MikeB
  • 37
  • 2
  • 9
  • Try just running one service with a timer in it that repeats every five minutes – JRowan Dec 27 '18 at 22:53
  • `Only Foreground Service is working but I cannot use it because user are not running activity, all is working in background using widget.` Are you sure? You don't have to use `Activity` to start Foreground service. If its not working, can you updated your question with code? – Sagar Dec 28 '18 at 01:23
  • Hi @Sagar, as my knowledge is correct, to run foreground service it is need to have activity opened or foreground service will closed. I tested it and foreground service started when activity was opened, and itworked, but when i closed activity then foreground service wasn't running every couple of minutes. – MikeB Jan 07 '19 at 11:30
  • @MikeB The foreground service doesn't require activity to be opened. It can outlive the activity lifecycle. – Sagar Jan 08 '19 at 02:27
  • @Sagar the foreground service remove from background when the app is removed from recent apps even when we use START_STICKY. Do you have any idea to prevent that. – JITHIN GOPAL May 06 '19 at 09:57

1 Answers1

0

You can use a foreground service for this. You just need to start the service as a foreground service like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(intent);
} else {
    context.startService(intent);
}

As soon as the service is started you need to call startForeground(int id, Notification notification).

That being said it can be nice to be aware of the issue at https://issuetracker.google.com/issues/76112072 .

Also take a look at the blog post at https://android-developers.googleblog.com/2018/12/effective-foreground-services-on-android_11.

They also seem to touch your use-case with tracking:

Passive location tracking is a bad use case for foreground services. If the user has consented to being tracked, use the FusedLocationProvider API to receive bundled location updates at longer intervals

You probably don't need to have an up to date widget when the screen is off.

Community
  • 1
  • 1
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76