一个看不到的东西叫Service
一个看不到的东西叫Service
官方文档是这么说:
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
?
小结说就是Service是运行在后台的一个组件,它可以“脱离“于应用而单独运行。
?
?
Sevice启动的形式:
?
1.使用 startService()
2.使用 bindService()
?
在探讨StartService之前,先看看它的祖先
?
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 that uses 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 implement onHandleIntent(), which receives the intent for each start request so you can do the background work.
?
Java世界告诉我们,你可以去继承它,然后使用它。具体详细使用不做介绍,可以参考google 官方API。
?
怎么去启动Service呢?之前提过“意图”这东西,使用它你可以启动一个你自己的服务。这些服务可以用于应用程序检测更新、后台广告、通知等等功能。例如Android开机自启动程序onBootReceive之后也是通过一个servcie实现后台运行。这些例子如qq、360的开机自启动。
?
//启动一个叫HelloService的程序,当然HelloService肯定是继承了它祖先Service或者IntentService特征。
//startServcie->onCreate->onStartCommand,通过onStartCommand 来启动它
Intent intent = new Intent(this, HelloService.class);
startService(intent);
?
// 这是一个“漫长“的生命周期 ……
// 最后 stopSelf() , stopService()来停止它。
?
?
继续探讨BoundServcie,直接看官方文档吧。
?
A bound service is one that allows application components to bind to it by calling bindService() in order to create a long-standing connection (and generally does not allow components to start it by calling startService()).
?
//step 1: bindServcie()
?
You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications, through interprocess communication (IPC).
?
//step 2: onBind()
To create a bound service, you must implement the onBind() callback method to return an IBinder that defines the interface for communication with the service. Other application components can then call bindService() to retrieve the interface and begin calling methods on the service. The service lives only to serve the application component that is bound to it, so when there are no components bound to the service, the system destroys it (you do not need to stop a bound service in the way you must when the service is started through onStartCommand()).
?
To create a bound service, the first thing you must do is define the interface that specifies how a client can communicate with the service. This interface between the service and a client must be an implementation of IBinder and is what your service must return from the onBind() callback method. Once the client receives the IBinder, it can begin interacting with the service through that interface.
?
Multiple clients can bind to the service at once. When a client is done interacting with the service, it calls unbindService() to unbind. Once there are no clients bound to the service, the system destroys the service.
// 系统不使用了,就会销毁
//step 3:unbindService()
?
There are multiple ways to implement a bound service and the implementation is more complicated than a started service, so the bound service discussion appears in a separate document about Bound Services.
?
?
题外话:IPC通信是一门必修课
?
最后看看两种方式创建的service的生命周期
?