Android Service run indefenitely

与世无争的帅哥 提交于 2019-11-30 20:44:05

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6581994/android-service-run-indefenitely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!