My service extends the MediaBrowserService class and it responds to certain MediaController actions such as getMediaController().getTransportControls().onPlay()
. However, I didn't find a way to "control" my service from a Widget, because the MediaController class isn't available from it. How can I communicate with my service?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The recommended approach is to use a MediaButtonReceiver, which will forward commands to your MediaBrowserService
, where you can then handle them by adding a call to MediaButtonReceiver.handleIntent() in your onStartCommand
as per the documentation.
You can use the 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()