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();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…