gpt4 book ai didi

android - 我需要为 Android 服务调用 unbindService 和 stopService 吗?

转载 作者:IT老高 更新时间:2023-10-28 23:18:59 25 4
gpt4 key购买 nike

在我的 Android 应用中,我同时调用 startServicebindService:

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/

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