安卓服务无限期运行[英] Android Service run indefenitely

本文是小编为大家收集整理的关于安卓服务无限期运行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我注意到Skype等应用程序使用基本上运行24x7的服务,而不会杀死.您甚至无法使用任务杀手手动杀死它(您可以通过转发服务并杀死服务来杀死它们).这是如何实施的?

我发现在Android 2.3中,我的服务在跑步后被杀死了.即使我使用start_sticky启动服务也从未被调用过ondestroy().但是,在我的2.1设备上工作正常,即服务并没有被杀死.

感谢

推荐答案

这是如何实现的?

基于显示Notification图标的Skype屏幕截图,然后它们很可能使用startForeground().

我发现在Android 2.3中,在某个时候运行后,我的服务会被杀死.

完全正常.

首先,大多数Android应用程序并不真正需要一个"基本上运行24x7"的服务.用户不喜欢这样的服务,这就是为什么任务杀手和运行服务屏幕和操作系统中的自动杀死逻辑存在.服务应该运行"24x7"的唯一原因是每微秒提供值. VoIP客户端,如Skype,将每微秒提供价值,因为它们正在等待来电.大多数Android应用程序不符合此标准.

如果您的服务不断运行,而且对于用户控制的时段(例如,音乐播放器),startForeground()是一个精细解决方案.

否则,我宁愿找到一种消除"基本上运行24x7"的服务的方法,使用AlarmManager切换到可用户控制的轮询系统,因此在交付时,您的服务通常不在内存中价值.

本文地址:https://www.itbaoku.cn/post/102690.html

问题描述

I notice that applications like Skype use a service which basically runs 24x7, without getting killed at all. You cannot even manually kill it using task killers ( you can kill them by going to running services and kill service ). How is this implemented?

I find that in Android 2.3, my service gets killed after running for sometime. onDestroy() is never called even if I start the service with START_STICKY. However this works fine on my 2.1 device, that is the service doesnt get killed.

Thanks

推荐答案

How is this implemented?

Based on the Skype screenshots that show a Notification icon, then they are most likely using startForeground().

I find that in Android 2.3, my service gets killed after running for sometime.

That is perfectly normal.

First, most Android applications do not really need a service that "basically runs 24x7". Users do not like such services, which is why task killers and the Running Services screen and the auto-kill logic in the OS exist. The only reason a service should be running "24x7" is if is delivering value every microsecond. VOIP clients, like Skype, will deliver value every microsecond, as they are waiting for incoming phone calls. Most Android applications do not meet this criterion.

If your service is running constantly, but for a user-controlled period (e.g., a music player), startForeground() is a fine solution.

Otherwise, I would rather that you find a way to eliminate the service that "basically runs 24x7", switching to a user-controllable polling system using AlarmManager, so your service is generally not in memory except when it is delivering value.