0

On startup i call bindService in onCreate() of my first activity so i can access my mediaplayer object which is a static object.

public static void bindMusicService(Context c){
        /*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
        mediaPlayerServiceIntent = new Intent(c, MediaPlayerService.class);
        c.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
        mServiceIsBound = true;
        Log.i("Main","Service is bound!");
}

Now when i click a song in my activity i call:

ContextCompat.startForegroundService(context, intent);

But what if the user doesn't click on a song and just closed the app? Do i still need to call unbindService(); in onPause()?

Because when the user does click on a song, it will show a notification which song is playing and when the app is moved to the background or swiped away and unbind is called, the notification won't work anymore.

Vince VD
  • 1,506
  • 17
  • 38
  • try this link https://stackoverflow.com/questions/5819368/android-service-can-be-bind-without-start – unzila Jul 03 '19 at 04:38
  • you always have to call `unbindService` - otherwise you will get memory leak – pskink Jul 03 '19 at 06:21
  • @pskink but still want to be able to control my music playback when my app is destroyed, when my app is destroyed i show a notification of the song that was playing with previous/next song and if i unbind these buttons don't work anymore. – Vince VD Jul 05 '19 at 21:54
  • a notification? so most likely you use `android.app.Notification.Builder` - your service does not need to be bound when it wants to use a `Notification` and `NotificationManager` – pskink Jul 06 '19 at 03:57

0 Answers0