22

I'm working on a music app, and I want stop the service when user removes the application from the recent list and not when the first activity is destroyed(because when user clicks back back until app minimizes, in that case first activity is also getting destroyed). Please help.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Naresh Gunda
  • 332
  • 1
  • 2
  • 12

3 Answers3

52

I want stop the service when user removes the application from the recent list.

Yes, you can either do that using stopWithTask flag as true for Service in Manifest file.

Example:

<service
    android:enabled="true"
    android:name=".MyService"
    android:exported="false"
    android:stopWithTask="true" />

OR

If you need event of application being removed from recent list, and do something before stopping service, you can use this:

<service
    android:enabled="true"
    android:name=".MyService"
    android:exported="false"
    android:stopWithTask="false" />

So your service's method onTaskRemoved will be called. (Remember, it won't be called if you set stopWithTask to true).

public class MyService extends Service {

    @Override
    public void onStartService() {
        //your code
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        System.out.println("onTaskRemoved called");
        super.onTaskRemoved(rootIntent);
        //do something you want
        //stop service
        this.stopSelf();
    }
}

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • 2
    While this perfectly answers the question, it does feel like it's not the best approach for a music app. Especially, if I want to do other stuff while still listening to music. I would strongly consider using a combination of a "Started"/"Bound" service with ongoing notifications – Leo Dec 16 '14 at 06:27
  • @Mystic Magic how can i stop playing music using media player as i have start playing sound from onRecieve of alarmclass extends with wakefulbroadcastreciever – Erum Feb 20 '15 at 11:15
  • 6
    why onTaskRemoved not called i implement same as you mention above – nilesh prajapati Feb 21 '18 at 06:51
  • @nileshprajapati what device did you test it on? – joao2fast4u Apr 11 '18 at 13:59
  • 1
    This will no longer work on API level >=26 because of background services limitation - https://developer.android.com/about/versions/oreo/background#services – Vadim Kotov Jan 11 '19 at 15:28
  • @VadimKotov Background services would be active for a minute in API level > 26. – Neeraj Sewani Dec 09 '19 at 06:13
  • @MysticMagicϡ Its not working for me.I am using api 27 – KJEjava48 Sep 25 '20 at 06:37
2

The last answer didn't work for me (I am using API 29). I looked up in the Javadoc of the onTaskRemoved function and saw the following instruction: "If you have set {android:stopWithTask}, then you will not receive this callback; instead, the service will simply be stopped."

So, all I got to do is to remove this line (android:stopWithTask="false") from the manifest and it worked just fine.

This is the updated code in the manifest file:

<service
android:enabled="true"
android:name=".ExitService"
android:exported="false"/>
Lahav
  • 21
  • 1
1

Im posting this answer, since the chosen as best solution was not working for me.

This is the updated version:

First create this service class:

 public class ExitService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        System.out.println("onTaskRemoved called");
        super.onTaskRemoved(rootIntent);
        //do something you want before app closes.
        //stop service
        this.stopSelf();
    }
}

Then, declare this way your service in the manifest label:

<service
            android:enabled="true"
            android:name=".ExitService"
            android:exported="false"
            android:stopWithTask="false" />

Now, just start the service wherever you want to do something before your app closing.

 Intent intent = new Intent(this, ExitService.class);
        startService(intent);
danielrosero
  • 596
  • 8
  • 14