gpt4 book ai didi

android - 我可以停止由 bindService 而不是 android 中的 StartService 启动的服务吗?

转载 作者:行者123 更新时间:2023-11-29 20:18:02 25 4
gpt4 key购买 nike

我将此“Activity 1”绑定(bind)到服务。我使用

启动了服务
Intent wrapperServiceIntent = new Intent(mContext,BleWrapper.class);
bindService(wrapperServiceIntent,mBLEWrapperServiceConnection,BIND_AUTO_CREATE);

我可以停止这个服务吗

stopService(new Intent(mContext,BleWrapper.class));

代替

mContext.unbindService(mServiceConnection);

上述情况是否可能?

最佳答案

来自文档:

当服务与所有客户端解除绑定(bind)时,Android 系统会销毁它(除非它也是使用 onStartCommand() 启动的)。因此,如果它是纯粹的绑定(bind)服务,您不必管理服务的生命周期 - Android 系统会根据它是否绑定(bind)到任何客户端来为您管理它。

但是,如果您选择实现 onStartCommand() 回调方法,那么您必须显式停止该服务,因为该服务现在被认为已启动。在这种情况下,服务会一直运行,直到服务通过 stopSelf() 或另一个组件调用 stopService() 自行停止,无论它是否绑定(bind)到任何客户端

关于android - 我可以停止由 bindService 而不是 android 中的 StartService 启动的服务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com