Android中Service(服务)详解

Service是Android中四大组件之一,在Android开发中起到非常重要的作用,先来看一下官方对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(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件。其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行。另外,一个组件能够绑定到一个service与之交互(IPC机制),例如,一个service可能会处理网络操作,播放音乐,操作文件I/O或者与内容提供者(content provider)交互,所有这些活动都是在后台进行。

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.

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.

通过startService()启动的服务处于“启动的”状态,一旦启动,service就在后台运行,即使启动它的应用组件已经被销毁了。通常started状态的service执行单任务并且不反悔任何结果给启动者。比如当下载或上传一个文件,当这项操作完成时,service应该停止它本身。

还有一种“绑定”状态的service,通过调用bindService()来启动,一个绑定的service提供一个允许组件与service交互的接口,可以发送请求、获取返回结果,还可以通过夸进程通信来交互(IPC)。绑定的service只有当应用组件绑定后才能运行,多个组件可以绑定一个service,当调用unbind()方法时,这个service就会被销毁了。

另外,在官方的说明文档中还有一个警告:

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not 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与activity一样都存在与当前进程的主线程中,所以,一些阻塞UI的操作,比如耗时操作不能放在service里进行,比如另外开启一个线程来处理诸如网络请求的耗时操作。如果在service里进行一些耗CPU和耗时操作,可能会引发ANR警告,这时应用会弹出是强制关闭还是等待的对话框。所以,对service的理解就是和activity平级的,只不过是看不见的,在后台运行的一个组件,这也是为什么和activity同被说为Android的基本组件。

Service生命周期中的一些方法:

Android中Service(服务)详解

通过这个图可以看到,两种启动service的方式以及他们的声明周期,bind service的不同之处在于当绑定的组件销毁后,对应的service也就被kill了。service的声明周期相比与activity的简单了许多,只要好好理解两种启动service方式的异同就行。

service生命周期也涉及一些回调方法,这些方法都不用调用父类方法,具体如下:

  1. public class ExampleService extends Service {  
  2.     int mStartMode;       // indicates how to behave if the service is killed   
  3.     IBinder mBinder;      // interface for clients that bind   
  4.     boolean mAllowRebind; // indicates whether onRebind should be used   
  5.   
  6.     @Override  
  7.     public void onCreate() {  
  8.         // The service is being created   
  9.     }  
  10.     @Override  
  11.     public int onStartCommand(Intent intent, int flags, int startId) {  
  12.         // The service is starting, due to a call to startService()   
  13.         return mStartMode;  
  14.     }  
  15.     @Override  
  16.     public IBinder onBind(Intent intent) {  
  17.         // A client is binding to the service with bindService()   
  18.         return mBinder;  
  19.     }  
  20.     @Override  
  21.     public boolean onUnbind(Intent intent) {  
  22.         // All clients have unbound with unbindService()   
  23.         return mAllowRebind;  
  24.     }  
  25.     @Override  
  26.     public void onRebind(Intent intent) {  
  27.         // A client is binding to the service with bindService(),   
  28.         // after onUnbind() has already been called   
  29.     }  
  30.     @Override  
  31.     public void onDestroy() {  
  32.         // The service is no longer used and is being destroyed   
  33.     }  
  34. }<

相关推荐