Services are an integral component of almost every android application. But the functionality they provide comes with a drain on battery and system resources. And if you’re not paying attention, services can easily stick around longer than they should which wastes system resources, and can often cause performance problems for your rendering thread. But Colt McAnlis has the answer : using services in the most efficient way possible means killing them off the right way, and sometimes, not even using them.
Video Link


On System Level, Services Ain’t Free

创建、销毁 Service 需要时间与内存


Service May Cost Frame Lost

  1. 作为 Local Service 启动,对应的 Service 运行在主进程的主线程上
  2. 作为 Remote Service 启动,对应的 Service 则是运行在独立进程的主线程上

对于1中的情况,如果 Service 中进行了耗时操作(超过了16ms),则在屏幕绘制时可能引起丢帧。


Don’t Use Services

if you don’t have to…

参见下一节罗列的 Solutions


Async Event Functions

  • GCM
  • BroadcastReceiver
  • LocalBroadcastReceiver
  • HandlerThreads
  • AsyncTaskLoaders
  • IntentService

Do not Let Services Live Longer Than They Needed

Service 有两种不同的启动方法,对应的种植方法也为两种

  • Started Services:在 Service 中调用stopSelf(),或者在外面调用stopService()来终止

  • Bound Services:通过unBindService()来终止

  • Mixed:如果是先通过startService()启动,再用bindService()绑定的 Service,那么在unBind()后,还要显示通过stopService()来终止


Tools


===Ending===