android service

Started
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

//started 开始的服务不需要给caller反馈信息
Bound
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

//bound的服务允许组件和她发送请求,得到结果


Caution: A service runs in the main thread of its hosting process—the service doesnot create its own thread and doesnot run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

//service 本身不新开线程,也不是在一个新的进程中。所以会产生ANP。所以做会产生ANP问题的任务时,"最好新开线程跑service"<<是在service中新开线程


Service

This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running.


IntentService

This is a subclass of Service thatuses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implementonHandleIntent(), which receives the intent for each start request so you can do the background work.

//如果不需要同时处理很多请求,这是个不错的选择。


Caution: A services runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow down activity performance. To avoid impacting application performance, you should start a new thread inside the service.



Extending the IntentService class


Because most started services don't need to handle multiple requests simultaneously (which can actually be a dangerous multi-threading scenario), it's probably best if you implement your service using theIntentService class.

// IntentService适合不需要同时处理多个请求的情况下,有一个工作队列来处理接受到的intent

The IntentService does the following:


  • Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.                                         //创建一个默认的工作线程来执行主线程通过onStartCommand()送过来的intents
  • Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.                 //创建一个工作队列
  • Stops the service after all start requests have been handled, so you never have to callstopSelf().                                                                                                          //当所有的请求被处理了会停止service,所以你不必自己call stopSelf()
  • Provides default implementation of onBind() that returns null.                                                                                                                                                                            //提供默认的onBind()
  • Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.                  //提供onStartCommand()来发送intent到工作队列,  再发送给onHandleIntent()处理




Figure 2. The service lifecycle. The diagram on the left shows the lifecycle when the service is created withstartService() and the diagram on the right shows the lifecycle when the service is created withbindService().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值