我已经实现了一个在后台运行音频的服务,该服务运行良好,但我无法从服务中获取 SimpleExoPlayer 的实例到活动以更新 UI 并且如果我退出并重新打开音频,则音频在后台播放两次活动。
音频播放器服务
public class AudioPlayerService extends Service {
private final IBinder mBinder = new LocalBinder();
private SimpleExoPlayer player;
private Item item;
private PlayerNotificationManager playerNotificationManager;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
playerNotificationManager.setPlayer(null);
player.release();
player = null;
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
enter code here
public SimpleExoPlayer getplayerInstance() {
return player;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle b = …
Run Code Online (Sandbox Code Playgroud) 我无法理解该bindServiceAsUser()
方法是用来做什么的。有人可以解释一下吗?谷歌搜索似乎没有多大帮助。
public boolean bindService(Intent intent, ServiceConnection connection, int flags) {
return mContext.bindServiceAsUser(intent, connection, flags, UserHandle.OWNER);
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的服务:
<service
android:name="com.myFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
然后我实现onBind
这样:
private final IBinder mBinder = new LocalBinder();
private myListener mListener;
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
public void setListener(myListener listener) {
mListener = listener;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (mListener != null) mListener.onMessageReceived(remoteMessage);
}
Run Code Online (Sandbox Code Playgroud)
这很简单:Activity绑定到Service并设置一个监听器.当服务收到消息时,它只是触发监听器
现在最大的问题是:如果活动突然崩溃会发生什么? 在那种情况下mListener
会指出一些不存在的东西,不是吗?
如何,在调用之前mListener.onMessageReceived(remoteMessage)
,我可以检查绑定的Activity是否还活着?
android ipc android-service android-service-binding bindservice
我想将应用程序实现/重构为Android Architecture Components概念,请参阅https://developer.android.com/jetpack/docs/guide
在本主题Android体系结构组件ViewModel-与Service / IntentService的通信中,我找到了关于体系结构问题的很好的答案,请参阅/sf/answers/3271530251/
但我想问一下,如何从存储库绑定服务,因为我们在这里既没有上下文也没有活动。要明确地说,问题在于如何合并这两个概念。
我现在是什么情况
我需要具有boundService(请参阅https://developer.android.com/guide/components/bound-services),该服务来自第三方作为库(我们称其为“第三方SDK”)。这个“第三方SDK”将在与某些外部硬件的蓝牙连接上做一些异步工作,因此它作为或多或少的永久后台服务运行。但是,将其实现为服务(意图服务,因此活动可以绑定到服务),并且我们必须通过实现自定义事件侦听器接口来接收事件。
我要怎么办
我也想使用架构组件。我定义了View和ViewModel,我想使用一个存储库作为“ Dagger2 Singleton”,该存储库提供本地存储以及Web服务调用的数据形式,请参阅https://developer.android.com/jetpack/docs/guide#fetch -数据
我的初衷是,我也可以将“第三方第三方SDK”作为某种形式的异步准远程数据源来处理,因此,存储库也应绑定到“第三方第三方SDK”。
不幸的是,我们通常需要以下代码将后台服务绑定到活动:
Intent csIntent = new Intent(XXX, ThirdPartyService.class);
YYY.bindService(csIntent, <instance of ServiceConnection>, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
其中XXX和YYY是上下文和活动(但不应同时出现在存储库中!)
问题是什么?
如果要从“架构组件存储库”访问该后台服务,必须如何根据https://developer.android.com/guide/components/bound-services修改以活动为中心的后台服务绑定的概念根据https://developer.android.com/jetpack/docs/guide#manage-dependencies实现为dagger2 @Singleton
不幸的是,我发现此问题的唯一半官方文件指出“应”进行演示(但票证已关闭):https : //github.com/googlesamples/android-architecture-components/issues/20
感谢您提供任何有关如何合并这两个概念的提示
android mvvm bindservice android-viewmodel android-architecture-components
我在 android 上使用 SpeechRecognizer 来识别用户的声音。在卸载 Google App 之前它运行良好。(https://play.google.com/store/apps/details?id=com.google.android.googlequicksearchbox&hl=en)
我更新了 Google App,但出现了“绑定到识别服务失败”等错误。如何使应用程序成功运行?
我该怎么做才能正常使用 SpeechRecognizer?
谢谢。
首先,我要说的是,我已经在SO上发现了很多与此问题几乎相同的问题,但是没有一个(我能找到的)解决了这个问题.这些是我看过的一些:
ServiceConnection.onServiceConnected()在绑定到已启动的服务 之后永远不会调用onServiceConnected,在bindService方法之后从未调用过 ServiceConnection :: onServiceConnected,即使Context :: bindService返回true也未调用? OnServiceConnected未被调用
这是我的Android应用程序代码的一部分:
//Local service interface
private INetworkQueryService myNetworkQueryService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Intent intent = new Intent(this, NetworkQueryService.class);
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
}
//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as …
Run Code Online (Sandbox Code Playgroud) android ×6
bindservice ×6
android-architecture-components ×1
exoplayer ×1
exoplayer2.x ×1
ipc ×1
mvvm ×1
service ×1