Android 服务 onCreate 被多次调用而不调用 onDestroy [英] Android service onCreate is called multiple times without calling onDestroy

查看:34
本文介绍了Android 服务 onCreate 被多次调用而不调用 onDestroy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

有什么想法吗?

推荐答案

当我的服务对活动使用相同的流程(默认)时,我遇到了同样的问题.但是当我让我的服务使用另一个进程时没有更多的问题.我编辑了我的 AndroidManifest.xml 如下...(添加了 android:process 属性)

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"/>

参见http://developer.android.com/guide/主题/清单/服务元素.html 信息.

这篇关于Android 服务 onCreate 被多次调用而不调用 onDestroy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆