My service:
public class MusicService extends MediaBrowserServiceCompat {
...
}
My Activity:
public class MediaActivity extends AppCompatActivity{
private MediaBrowserCompat mMediaBrowser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMediaBrowser = new MediaBrowserCompat(this,
new ComponentName(this, MusicService.class), mConnectionCallback, null);
mMediaBrowser.connect();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMediaBrowser.disconnect();
}
I want to add button close in UI, but how to stop MusicService
? It continues casting in background.
I've declared a method in
MediaSessionConnection
:And in
MusicService
:I do not claim, that this is the correct way of stopping the service completely, but this is the solution that I've came up to after struggling some hours.
If
mediaBrowser.disconnect()
is not executed, thenstopSelf()
won't destroy the service (by telling "destroy the service" I anticipateonDestroy()
to be called).Without explicitly performing
stopForeground()
andstopSelf()
simply performingmediaBrowser.disconnect()
wouldn't destroy the service.