3

first of all I'd like to point out that I've done much of research here on stack overflow, on the android documentation and other sites but I still haven't found a solution.

I need your help to find out how to keep my SportTracker service running in background in android >= O. Actually I start the service as a sticky foreground service with :

Intent intent = new Intent(mContext, SmanAppLocationSerivce.class);
intent.putExtra(RequestCode.AgentService,RequestCode.START_SPORT_NOTIFICATION);
startForegroundService(intent);

In my service I do :

public void onCreate() {

    super.onCreate();

    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setFastestInterval(Project.TIME_GPS_UPDATE_FAST)
            .setInterval(Project.TIME_GPS_UPDATE)ì;



    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

and in the onStartCommand method I startForeground the service.

In the onLocationChanged I need to calculate the time elapsed since start, distance and save the new location to db so i can get them all when I save the sport session and send them to server.

I also need to update the ongoing notification every second with a timer so I can show the session time and session distance.

(The best example I can give you for the expected behaviour a sport session of Runtastic)

Actually all works fine, if I stop my activity the service keeps running as he's supposed to do but when the phone goes to sleep (i suppose when goes to doze mode) the service got killed by the system.

I tried these solutions :

  • I tried to set an Alarm every n seconds to check if the service is active and ( if it's dead ) restart it. NOT WORKING, seems like even the alarm is killed by the system ( even using setExactAndAllowWhileIdle )
  • Tried to use a broadcast receiver for ACTION_SCREEN_OFF and ACTION_SCREEN_ON so I can stop the timer for the notification update on screen off and restart it on screen_on to show the new data ( not working )
  • Tried to use a different priority for the location request (PRIORITY_BALANCED_POWER_ACCURACY instead of PRIORITY_HIGH_ACCURACY ). Sometimes the service survives, sometimes he got killed but the accuracy of locations point is very low and, since I'm tracking a sport session, I need the high accuracy
  • I tried to ask the user to ignore battery optimization for my app redirecting to the settings view with Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS. Even with the app ignoring battery optimization the service ( with high accuracy ) got killed in doze mode.

Someone got an idea on how to solve this? Maybe it just need to be done in some pattern that I don't know to work with O.

Thank you all Bye

UPDATE

I still haven't found what I'm looking for ( U2 :) ). I ended up asking the user to manually set the app on protected apps list, so the power manager doesn't kill it.

You can find some usefull info here "Protected Apps" setting on Huawei phones, and how to handle it

  • Is this behavior common for every phone you test? – Andrea Scalabrini Sep 12 '18 at 15:08
  • actually we noticed this behaviour most on Huawei (and honor) phones with Oreo but it seems common for other oreo phones, yes – Fabio Pocci Sep 12 '18 at 15:50
  • @FabioPocci Have you sorted this issue? If so kindly update how you have sorted it. As I am also facing similar issues. – DSP Jan 05 '19 at 04:02
  • Hello @DSP , no sorry, I ended up asking the user to set my app to protected apps for some vendors ( huawei, xiaomi etc ) with their own power management. You cand find some info here https://stackoverflow.com/questions/31638986/protected-apps-setting-on-huawei-phones-and-how-to-handle-it/35220476#35220476. If you ever find a better solution let me know please! :) – Fabio Pocci Jan 06 '19 at 10:35

0 Answers0