Android服务onCreate被多次调用而没有调用onDestroy

发布于 2024-12-02 01:22:26 字数 304 浏览 1 评论 0原文

在我的应用程序中,我使用一项服务与我们的服务器进行通信。该服务生成多个消息队列线程来处理具有不同优先级的任务。

该模型已经使用了大约一年,没有出现大问题。然而,最近,我发现有时我的服务类的 onCreate 被多次调用。 onDestroy 绝不会在两次 onCreate 调用之间调用。因此,我没有机会杀死现有线程。一旦发生这种行为,服务内部就有重复的线程。

我唯一改变的是将服务作为前台服务运行,即用户登录应用程序。我想知道是否前台服务导致了问题。

有什么想法吗?

In my app, I use a service to communicate with our server. The Service spawns several message queue threads to deal with tasks with different priorities.

This model has been used for about one year without big issues. However, recently, I found some time the onCreate of my service class are called multiple times. onDestroy is never called between two onCreate calls. Therefore, I did not get chance to kill existing threads. Once this behavior happens, the service has duplicate threads inside.

The only thing I have changed is to run the service as foreground service is a user signs in the app. I am wondering whether foreground service cause the problem.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2024-12-09 01:22:27

当我的服务使用与活动相同的流程(默认)时,我遇到了同样的问题。但当我让我的服务使用另一个进程时,就不再出现问题了。我像下面一样编辑了 AndroidManifest.xml...(添加了 android:process 属性)

<service android:name="kr.co.pkbio.binoo.CacheFileManagerService" android:process=":kr.co.pkbio.binoo.service"/>
<service android:name="kr.co.pkbio.binoo.ActivityStackManagerService" android:process=":kr.co.pkbio.binoo.service"/>

请参阅 http://developer.android.com/guide/topics/manifest/service-element.html 了解信息。

I had the same problem when my service used the same process with activities(default). but no more problems when I made my service use another process. I edited my AndroidManifest.xml like below... (added android:process attribute)

<service android:name="kr.co.pkbio.binoo.CacheFileManagerService" android:process=":kr.co.pkbio.binoo.service"/>
<service android:name="kr.co.pkbio.binoo.ActivityStackManagerService" android:process=":kr.co.pkbio.binoo.service"/>

see http://developer.android.com/guide/topics/manifest/service-element.html for information.

水染的天色ゝ 2024-12-09 01:22:27

当我处理蓝牙服务时,我遇到了完全相同的问题(问题发生在开发过程中)。当我断开设备连接并终止 ADB 时,这种情况不再发生,并且 onCreate 仅被调用一次。我认为这是一些错误,因为它时常发生。但也许你有一个稍微不同的问题......

I had exactly the same problem when I was dealing with a bluetooth service (the problem occurred during the development). When I disconnected my device and killed the ADB it didn't happen again and onCreate was called only once. I assume it is some bug because it happens from time to time. But maybe you have a slightly different problem...

蓝海 2024-12-09 01:22:27

这可能对某人有用。
在我的一项活动的 onSaveInstanceState 方法中通过捆绑包存储地图时,我遇到了同样的问题。我相信,因为地图包含对应用程序其他部分(其中之一是我的服务)中使用的对象的引用,所以每次我离开所述活动时,我的服务都会重新开始。

我猜这是 Android 操作系统的恶作剧。

This might become useful to someone.
I had this same issue when storing a map through a bundle in a onSaveInstanceState method in one of my activities. I believe because the map contained references to objects that were being used in other parts of the app (one of them was my service) every time I left said activity, my service would start all over again.

I'm guessing this is android OS shenanigans.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文