简体   繁体   English

如何将小部件连接到用于播放音乐的后台服务?

[英]How do I connect widget to my background service which is used to play music?

I'm trying to build a basic music player but right now I'm stuck at making widget for it. 我正在尝试构建一个基本的音乐播放器,但现在我仍然坚持为其制作小部件。 Can someone tell me how do I connect widget to the background service? 有人可以告诉我如何将小部件连接到后台服务吗? I'm using MediaBrowser,MediaController on the client side and MediaBrowserService,MediaSession in the background service. 我在客户端使用MediaBrowser,MediaController,在后台服务中使用MediaBrowserService,MediaSession。 So, to create a instance of MediaController, I require activity which isn't available in widget class if I'm not wrong and if I try to bind widget class to service using bindService and returning an instance of the background service class method I'm getting error (which is resolved once I comment the onBind method in the service) 因此,要创建MediaController的实例,我需要活动,如果我没有记错,并且如果我尝试使用bindService将小部件类绑定到服务并返回背景服务类方法的实例,则该活动在小部件类中不可用m出现错误(一旦我在服务中注释了onBind方法,该错误就会解决)

How do I proceed? 我该如何进行?

AppWidgetProvider is a BroadcastReceiver on steroids. AppWidgetProvider是类固醇上的BroadcastReceiver But you can't bind to service from BroadcastReceivers (That was done because BroadcastReceiver is lightweight component that have shot livetime (10 seconds)). 但是您不能绑定到BroadcastReceivers的服务(之所以这样做,是因为BroadcastReceiver是轻量级的组件,具有实时播放时间(10秒))。 In your case best way to communicate with music service is use broadcasts (pending intents). 在您的情况下,与音乐服务进行通讯的最佳方法是使用广播(有待处理的意图)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用后台服务播放音乐 - play music with background service 如何使我的Android应用程序播放手机音乐播放器中的音乐? - how do i make my android application play music from the phone's music player? 如何打开外部音乐播放器(例如Pulsar,Retro音乐播放器)并播放从列表视图中选择的歌曲? - How to open external music players (such as Pulsar, Retro Music Players) and play a song which i have selected from my listview? Android Studio:如何从用于设置按钮背景的可绘制对象中删除白色背景? - Android Studio: How do I remove the white background from my drawable, which is used to set the background of my button? 如何在后台检测音乐播放 - How to detect that music play in background 启动服务播放背景音乐时出错 - Starting a Service to play background music is getting errors 每当我按屏幕时如何停止重复播放背景音乐? - How do I stop my background music from repeating whenever I press my screen? 如何在我的Android应用中使用下载的Google Play商店音乐? - How do I use downloaded Google Play Store music in my android app? 如何停止音乐背景服务? - How to Stop Service of Music Background? 为什么我们需要Android中的服务?音乐播放也可以通过后台线程来完成 - Why do we need a service in android? Music play can be done by background thread also
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM