如何将 MediaBrowserService 与小部件一起使用?

How can I use a MediaBrowserService with a widget?

我的服务扩展了 MediaBrowserService class 并且它响应某些 MediaController 操作,例如 getMediaController().getTransportControls().onPlay()。但是,我没有找到从 Widget "control" 我的服务的方法,因为 MediaController class 不可用。我如何与我的服务通信?

推荐的方法是根据文档在 onStartCommand 中使用 MediaButtonReceiver, which will forward commands to your MediaBrowserService, where you can then handle them by adding a call to MediaButtonReceiver.handleIntent()

您可以使用 buildMediaButtonPendingIntent() to construct a PendingIntent for various media buttons (such as play, pause, etc), which can then be set on your widget's buttons with setOnClickPendingIntent()