如何访问MediaBrowserServiceCompat服务的实例? [英] How to access instance of MediaBrowserServiceCompat service?
问题描述
我很努力地掌握从MediaBrowserServiceCompat
派生的服务实例.
I'm surprisingly struggling to get hold of the instance of a service which is derived from MediaBrowserServiceCompat
.
对于典型的服务,要实现此目的,使用本地活页夹
For a typical service, to achieve that, a local binder is used
class MyService extends MediaBrowserServiceCompat {
class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
private final MyBinder binder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void doMagic() { // <-- How to call this from activity?
// ...
}
}
// At the activity, for example, this binder is retrieved through
// `onServiceConnected()`, typecast to `MyBinder` and thus
// `getService()` gets us the service instance.
但是,这不适用于从MediaBrowserServiceCompat
派生的服务.
However, this does not work for the service derived from MediaBrowserServiceCompat
.
当我尝试提供本地联编程序时,如上所述,由于MediaBrowserServiceCompat
期望它自己的自定义联编程序具有附加功能,服务崩溃了.这是崩溃堆栈跟踪
When I try to provide local binder, as shown above, service crashed since MediaBrowserServiceCompat
expects its own custom binder with additional functionality. Here is the crash stacktrace
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.os.Binder.queryLocalInterface(Binder.java:254)
at android.service.media.IMediaBrowserService$Stub.asInterface(IMediaBrowserService.java:31)
at android.media.browse.MediaBrowser$MediaServiceConnection.onServiceConnected(MediaBrowser.java:793)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1223)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1240)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我尝试包装MediaBrowserServiceCompat
超类中的活页夹,如下所示,但仍然无法正常工作.
I tried wrapping the binder from MediaBrowserServiceCompat
super class, like below, but still it wouldn't work.
class MyService extends MediaBrowserServiceCompat {
class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
private final WrappingBinder<MyService> binder;
@Override
public IBinder onBind(Intent intent) {
if (binder == null) {
binder = new WrappingBinder(super.onBind(intent));
}
return binder;
}
public void doMagic() { // <-- How to call this from activity or elsewhere?
// ...
}
}
所以想知道如何访问MediaBrowserServiceCompat服务实例吗?
So wondering what is the approach to follow to access the instance of MediaBrowserServiceCompat service?
需要明确的是,播放功能对我来说很好用.问题是关于如何获得对服务的引用,以便我可以对该服务上的自定义公共方法进行调用.
To be clear, Playback functionality is working fine for me. The question is about how to get a reference to the service, so that I can make calls to my custom public methods on that service.
推荐答案
您发现,MediaBrowserServiceCompat
已经是绑定服务-您不能也不应该覆盖onBind()
.
As you discovered, MediaBrowserServiceCompat
is already a bound service - you cannot and should not override onBind()
.
Instead, you must connect to your MediaBrowserServiceCompat
with a MediaBrowserCompat
as per the documentation. Once connected, you can trigger custom methods, like doMagic
by:
- 根据
sendCommand
,传入唯一标识您的命令(例如,doMagic
),希望传递给该方法的任何参数的command
字符串,以及ResultReceiver
如果要返回值. - 在您的
MediaBrowserServiceCompat
注册的MediaSessionCompat.Callback
中,覆盖
- Create a
MediaControllerCompat
from yourMediaBrowserCompat
instance, following the documentation - Call
sendCommand
, passing in acommand
String which uniquely identifies your command (say,doMagic
), any parameters you wish to pass to the method, and aResultReceiver
if you want a return value. - In the
MediaSessionCompat.Callback
registered with yourMediaBrowserServiceCompat
, override onCommand() and handle the command (say, by callingdoMagic
).