Being an Android service is a little like being in a Final Destination movie. You'll be minding your own business, doing all sorts of useful stuff in the background, then with no warning the Activity Manager decides to push you down an elevator shaft onto some pointed objects that have no business being there.

OK, so there is a solid justification for why Android kills background services all the time. It's a mobile OS, so performance and battery life are a big concern. But that doesn't stop a lot of people from asking, "How can I keep my services from getting killed?"

The general consensus seems to be that it's not possible, which is mostly true. But there are a few options available.

If you're a system developer (like I am at the moment), you're really in luck. The <application> tag in the Android manifest has a poorly-documented but extremely useful attribute, android:persistent. Setting this to "true" makes your app literally impervious to death...with 2 important caveats:
It only applies to system apps (ie apps in /system/app). The flag is ignored for apps in /data/app. So as I said, this is only useful if you're developing an Android system, or maybe if you're manually installing an app on a rooted phone. Marketplace apps are right out of luck.
Persistence only applies to services in the default application process. If any <service> tag includes an "android:process" attribute that causes it to run in a separate process, that process will NOT be persistent.
You can check persistence with the adb shell command "dumpsys activity". Persistent processes will be marked with "PERS":
> adb shell dumpsys activity | grep PERS
PERS #17: adj=sys /F ae27b4d0 340:system/1000 (fixed)

Obviously this should be used with extreme care. Any non-critical tasks in your app should be moved to a separate service with the "android:process" attribute set, so that those particular bits are not persistent.

For Marketplace developers...sorry, but your services still have a bullseye on them. It's not all bad though; there are 3 solid (and well documented) solutions that should suffice:
If you need to perform a task repeatedly at set intervals (ie polling for data), set an AlarmManager to spawn your service as needed.
If you really need to remain running constantly (ie to maintain music playback or something else with zero interruptions), call startForeground(). In newer Android flavors, you'll have to provide a Notification to alert the user that your service is running...but that's not much to ask.
If you need to remain running constantly but can endure short interruptions, just return START_STICKY from your onStartCommand() method. If Android does kill your service, it will be restarted ASAP (with calls to onCreate and onStartCommand). In my experience it's rarely more than a minute of downtime, but this could vary.
Note: if you are writing an older app that uses onStart instead of onStartCommand (or if you return the START_STICKY_COMPATIBILITY flag instead), your service will be restarted with a call to onCreate only (onStart will not be called for a restart).

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 【android学习笔记】收藏的网上查找的and
  2. Android TV 焦点控制逻辑
  3. java
  4. android sdk 文件目录含义介绍
  5. __builtin_clz(); __builtin_ctz(); __bu
  6. android 布局边框阴影1dp
  7. 【源码分享下载】每日更新之Android快速
  8. android通知栏进度条
  9. Android 之 shape和selector用法介绍
  10. Android(安卓)Color值汇总