- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在我的 Android 应用中,我同时调用 startService
和 bindService
:
Intent intent = new Intent(this, MyService.class);
ServiceConnection conn = new ServiceConnection() { ... }
startService(intent)
bindService(intent, conn, BIND_AUTO_CREATE);
稍后,我尝试同时 unbindService 和
stopService`:
unbindService(conn);
stopService(intent);
但是,我在调用 unbindService
时遇到异常。如果我删除此调用,应用程序似乎可以通过 stopService
调用正常运行。
我做错了吗?我认为 bindService
调用必须与 unbindService
调用相关联,而 startService
调用必须与 stopService
调用。不过,这里的情况似乎并非如此。
最佳答案
stopService() 的 Android 文档指出:
Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle.
所以首先调用 stopService()
然后调用 unbindService()
应该可以工作(它对我有用)。
关于android - 我需要为 Android 服务调用 unbindService 和 stopService 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3385554/
我已经在我的android项目中使用Binder实现了进程间通信。我的 Activity 和服务正在运行相同的应用程序。我已通过调用 unbindService(intent) 与服务断开连接,并且服
我有一项服务将 Messenger 存储为成员变量并在 onBind(Intent) 上返回 messenger.getBinder()。我通过以下方式连接到此服务: void Bind() {
从所有代码示例来看,如果我们调用了#bindService,我们只想调用#unbindService(在同一上下文中),这是通过 boolean 检查完成的。 但是在#bindService 调用中没
对于过去偶然发现类似疑问的人来说,这更多是关于 Android 内部结构的问题。 问题本质上很简单: 假设我有一个绑定(bind)服务,可以向在其他进程中运行的应用程序提供某些功能 - 我如何知道请求
以下代码来自 project . 启动 bindService() 时会显示通知图标, 没关系。 但是当我启动 unbindService() 时通知图标不会被清除, 为什么?我在以下代码中找不到任何
我的应用程序使用另一个(我的)应用程序提供的服务。我正在使用 bound service使用 Messenger 访问它并与其通信(由于它是一个不同的应用程序,它也是一个远程服务)。 当我以正确的 I
我正在绑定(bind)到 android 服务,如 JavaDoc 中所示 private boolean bound = false; private MyService service = nul
在我的 Android 应用中,我同时调用 startService 和 bindService: Intent intent = new Intent(this, MyService.class);
我正在编写一个简单的音乐播放器,我已经创建了一个播放服务,它实现了一个 AIDL 接口(interface)来与客户端绑定(bind),一个是简单的轨道浏览器,另一个是更简单的播放器 Activity
我在我的应用中使用 AdMob。我遵循了实现指南,横幅正确显示和加载了广告。我的 MainActivity 负责创建引用、构建 AdRequest 和显示横幅,以及暂停、恢复和销毁 AdView。每次
我是一名优秀的程序员,十分优秀!