I use startForeground to make my service "persist" in background and not be killed by the OS.
I remove the service in the main activity onDestroy method by calling stopForeground and stopService.
The problem is, when I swipe my app off the recent apps to kill it, the debug session is still running, whereas in the "normal" functioning (without using startForeground), the debug session terminates correctly.
Using adb shell confirms that the app is still running.
startForeground somehow creates a "special" running thread that could not be stopped by simply stopping the foreground and the service.
Any ideas please ?
Asked
Active
Viewed 1.5k times
12

Phantômaxx
- 37,901
- 21
- 84
- 115

MMasmoudi
- 508
- 1
- 5
- 19
3 Answers
32
if you want to stop your service when you are clearing your application from the recent task, you have to define an attribute stopWithTask
for service in the manifest file like this as shown below
<service
android:enabled="true"
android:name=".ExampleService"
android:exported="false"
android:stopWithTask="true" />
then you can override onTaskRemoved method in the service , this will be called when the application's task is cleared
@Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want
//stop service
this.stopSelf();
}

Hasif Seyd
- 1,686
- 12
- 19
-
Thank you ! That's exactly what I wanted to do :) – MMasmoudi Nov 16 '18 at 13:25
-
+1: This helped me as well, thank you! Can you add a reference, because I can't find `stopWithTask` in official documentation? – Astrogator Jun 03 '21 at 16:20
-
3Be careful! According to the docs `onTaskRemoved()` will not be called if you've set `stopWithTask=true` in the manifest (https://developer.android.com/reference/android/app/Service#onTaskRemoved(android.content.Intent)) – Josselin Apr 06 '22 at 11:39
8
I don't know if it's correct but on my app i'm stopping the foreground service here and it works.Check the code
private void stopForegroundService() {
// Stop foreground service and remove the notification.
stopForeground(true);
// Stop the foreground service.
stopSelf();
}
UPDATE
Call the stopservice
from your main class somehow(not from onDestroy) like this:
Intent intent = new Intent(this, MyForeGroundService.class);
intent.setAction(MyForeGroundService.ACTION_STOP_FOREGROUND_SERVICE);
startService(intent);
MyForegroundService.java
private static final String TAG_FOREGROUND_SERVICE = "FOREGROUND_SERVICE";
public static final String ACTION_START_FOREGROUND_SERVICE = "ACTION_START_FOREGROUND_SERVICE";
public static final String ACTION_STOP_FOREGROUND_SERVICE = "ACTION_STOP_FOREGROUND_SERVICE";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();
switch (action) {
case ACTION_START_FOREGROUND_SERVICE:
startForegroundService();
break;
case ACTION_STOP_FOREGROUND_SERVICE:
stopForegroundService();
break;
}
}
return START_STICKY;
}
private void stopForegroundService() {
Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");
// Stop foreground service and remove the notification.
stopForeground(true);
// Stop the foreground service.
stopSelf();
}

Alex
- 1,816
- 5
- 23
- 39
-
1I have just tried it and the output is the same. Did you try to kill your app by swiping it off the recent apps while debugging and while your service is running ? Just to see if the debug session is properly stopped – MMasmoudi Nov 16 '18 at 08:57
-
@MahmoudMasmoudi You said you've implemented it in onDestroy()? try call this method from inside your service class.Check the updated answer.I hope it helps – Alex Nov 16 '18 at 09:11
-
Thank you for your reply. This should work. However, it can not detect when the user has swiped off the app from "recent apps" (That's why I had to put this in the activity's onDestroy method) – MMasmoudi Nov 16 '18 at 13:24
-
I'm sorry i don't know how to help you more :/ I hope you finally find your answer – Alex Nov 16 '18 at 14:45
-
-
Why can we not stop foreground services using `mContext.stopService(mIntent);`? – Akshay Choudhry Dec 28 '21 at 12:28
-
is it possible to terminate the foreground by other activity that actually didn't create 'em at the first time? – gumuruh Apr 26 '22 at 04:51
0
Another way (for library service):
val cmp = ComponentName(this, XXXService::class.java)
packageManager.setComponentEnabledSetting(
cmp, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
The service will be stopped immediately.
Then you can set PackageManager.COMPONENT_ENABLED_STATE_ENABLED
when needed

sowhat
- 131
- 1
- 6