Search code examples
javaandroidforeground-serviceandroid-fusedlocation

LocationUpdatesForegroundService app process is still running after closing app


I am doing some investigation on Google Play Location Service Google Play Location Samples

I am experiment the following issue on LocationUpdatesForegroundService sample application: the app process is still running after closing the app.

Steps to reproduce it:

  • Open app
  • Press "request location updates" button
  • Press device home button for sending application in background
  • Close the app

My expectation is that the app process (and all its threads) has to be stopped but it does not.

The app process is still running after closing the app

It happens when I close the app after calling

startForeground(NOTIFICATION_ID, getNotification());

How can avoid it?

It looks related with the foreground service for collecting location. How can I stop the service while the app is closing?


Solution

  • As per the documentation the Service is the Android component which run continues in background even if the app is closed.

    If you want to stop service when app is remove from recent task(i.e app kill) you have to used android:stopWithTask="true" in AndroidMenifest.xml for explicitly closed the service.

    Example ,

    <service
            android:name=".LocationUpdatesForegroundService"
            android:stopWithTask="true" />