如何访问MediaBrowserServiceCompat服务的实例? [英] How to access instance of MediaBrowserServiceCompat service?

本文介绍了如何访问MediaBrowserServiceCompat服务的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很努力地掌握从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:

  1. 根据
  1. Create a MediaControllerCompat from your MediaBrowserCompat instance, following the documentation
  2. Call sendCommand, passing in a command String which uniquely identifies your command (say, doMagic), any parameters you wish to pass to the method, and a ResultReceiver if you want a return value.
  3. In the MediaSessionCompat.Callback registered with your MediaBrowserServiceCompat, override onCommand() and handle the command (say, by calling doMagic).

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆