Title: Is there any way to not stop service even if app is stopped through the multitasking screen?
Tags: android-service
Question: My service gets stopped when app is closed.

Code already provided.

My Service code is:

```public class MusicService extends Service {

    MediaPlayer myPlayer;

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

        myPlayer = MediaPlayer.create(this, R.raw.nokiatune);
        myPlayer.setLooping(false); // Set looping
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        myPlayer.start();
    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
        myPlayer.stop();
    }

}
```

I have developed a Service in android. It is working fine. The only thing is that service stops when app is closed through the multitasking screen. Is there any way to not stop the service even if the app is closed through the multitasking screen ?



Here is another answer: You could try the approach discussed here. Basically, you register a BroadcastReceiver that restarts your Service if it is destroyed. On your ```AndroidManifest.xml```:

```<receiver
     android:name="yourpackagename.RestartServiceBroadcastReceiver"
     android:enabled="true"
     android:exported="true"
     android:label="RestartServiceWhenStopped"&gt;
</receiver
```

Your ```BroadcastReceiver```:

```public class RestartServiceBroadcastReceiver extends BroadcastReceiver {
 @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, MusicService.class));
    }
}
```

Then on your Service's ```onDestroy```, you send a Broadcast so that the BroadcastReceiver can restart your service. 

```@Override
    public void onDestroy() {
          Intent broadcastIntent = new Intent(this,RestartServiceBroadcastReceiver .class);
          sendBroadcast(broadcastIntent);
          myPlayer.stop();
    }
```

Also, you have to move your logic inside the ```onStart``` to your Service's ```onStartCommand``` and have it return ```START_STICKY```, like this:

```@Override
public int onStartCommand() {
    myPlayer.start();
    return START_STICKY;
}
```

Returning this tells Android not to kill your service--however, there's no guarantee that Android will honor this and can still kill your service. 

Finally in the Activity where you start the Service, you need to make sure that you check first if the service is already existing before starting the service, and stop the service during ```onDestroy```, so that the BroadcastReceiver can restart the service.

A warning though: this approach will not work above Android O, please see this for more details.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:09/13/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android-service>', 'Title': 'Is there any way to not stop service even if app is stopped through the multitasking screen?', 'ViewCount': 30, 'id': 57921602, 'user_AboutMe': None, 'user_AccountId': 1452467.0, 'user_DisplayName': 'user1368974', 'user_Reputation': 1.0, 'user_Views': 6.0, 'user_id': 1368974.0}}