开发者

Understanding android service

开发者 https://www.devze.com 2023-04-10 02:29 出处:网络
From here, It said Service is not a separate process and it isnot a thread. But how can i开发者_开发知识库t \"A facility for the application to tell the system about something it wants to be doing i

From here,

It said Service is not a separate process and it is not a thread. But how can i开发者_开发知识库t "A facility for the application to tell the system about something it wants to be doing in the background" ?

How can something being done in background without being a process or a thread?


A service is more or less a potentially-long lived Android object. In other words, Android will instantiate a service object for you and keep it alive for you until you tell Android you are done with it.

It is common for services to start a background thread to perform some task. Once they complete their task, they tell Android that the service can be stopped. Once your service is stopped, Android will call onDestroy on it. Now that Android knows your service is done, if the rest of your application isn't running, Android may decide to destroy the process your application was using to reclaim memory. It wasn't doing this before since your service was running.

Think of it this way (note, some things are left out as to not overwhelm you):

  1. Android creates an instance of your service
  2. Android calls onStart on your service and you start a thread
  3. Execution returns back to Android and it sees your service is still running so it doesn't call onDestroy
  4. Some time passes where your background work is being done
  5. You call stopService on your service or your service is unbound
  6. Android now sees the service object can be destroyed and collected
  7. Android calls onDestroy on your service and removes it from its own internal list of running services


How can something being done in background without being a process or a thread?

Simple, is not a process but is part of some process. This means there could very well be a single process that handles all services in the system, similar to how widgets are handled at the home activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消