Reputation: 11
I am developing an android auto but i have some problems in this part of my code in Onbind
method of the Service
:
public IBinder onBind(Intent arg0) {
Log.i("TAG", "OnBind");
// TODO Auto-generated method stub
if (SERVICE_INTERFACE.equals(arg0.getAction())) {
Log.i("TAG", "SERVICE_INTERFACE");
registerReceiver(receiver, filter);
return super.onBind(arg0);
}
else{ Log.i("Musica Service", "musicBind");
return musicBind;}
}
I have another activities that bound with my service through a musicBind
IBinder
, but in the other hand I have set all things to connect my app in Android auto interface but after close my app after disconnect the device from the Android auto I cant stop my MediaBrowserServiceCompat
I think is due to this SERVICE_INTERFACE
keeps binded the Service .
How can I stop or destroy this from the same Service MediaBrowserServiceCompat
?
Upvotes: 1
Views: 526
Reputation: 199825
Per the media browser service lifecycle documentation, it is common for your MediaBrowserServiceCompat
to be only bound (therefore being destroyed when the last client unbinds), only started (such as when playing music in the background), or both bound and started (such as when playing music while your UI is visible).
Your service will only be destroyed when it 1) no longer has any bound clients and 2) when you call stopSelf()
(assuming it was at some point started). It is not likely that Android Auto, having worked with hundreds of other apps, would continue to bind to your service and not be a serious issue that applies to all media apps.
Therefore it is much more likely that your service has been started somehow. Per the Media Session callbacks documentation, you should be calling startService
when you start playback and stopSelf
when your stop playback - this ensures that your service, assuming no one is still bound to it, will be destroyed when playback is stopped.
You can look at the status of your service by using adb shell dumpsys activity services
(optionally adding the name of your service's class to filter for just that class).
Upvotes: 2