Android Service run indefenitely

放肆的年华 提交于 2019-12-01 01:39:34

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.

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