Android中的Service详解2

来源:互联网 发布:浅喜似苍狗 知乎 编辑:程序博客网 时间:2024/06/10 20:12

1、什么是Service

     Service的官网解释是:Service是一个在后台执行较长时间操作且不提供用户界面接口的应用程序组件。其它的应用程序组件能开启一个Service,Service将在后台运行甚至用户切换到其它应用时也有效。另外,一个组件可以绑定一个Service并与之相互通讯,同时也可以执行一些进程间的通讯操作。在 Service中主要执行一些比较耗时的操作,诸如网络下载,音乐播放、I/O文件操作和与Content Provider之间的联系等,当然所有的操作都是在后台进行。需要注意的是,Service即不是进程,也不是线程,它是运行在其所在宿主进程的主线程中。

2、Service的生命周期

      使用Service一般分为两种方法,Started Service和Bound Service:
Started Service:
     当一个应用程序组件调用startService()方法时便开启了一个Service,一般来讲,应用程序组件(比如Activity)一旦开启了一个Service,便和Service没有什么联系,甚至该应用程序组件销毁了,Service仍然正常运行直到组件调用stopService()方法或者是Service中的任务执行完成后自动停止。Service一般是运行在后台的操作,它不与前台进行交互或是返回任何值,比如网络的上传和下载,一旦任务完成Service变回自动停止销毁。
Bound Service:
     当一个应用程序组件调用boundService()方法时便绑定了一个Service.Bound Service 实现了前台应用程序组件和后台Service的通讯,两者之间可以相互发送请求,获得结果甚至执行进程间的通讯(IPC)。Bound Service是依存于前台应用程序组件的,当前台组件销毁了,绑定的Service也会随之销毁,前台的多个组件可以绑定后台同一个Service,不过当所有的前台组件都解除绑定后,Service会被Destory掉。

两者Service的使用方法与Activity类似,都管理着各自的生命周期,下面是两者的生命周期图示:


       Started Service经历创建-->运行-->销毁(onCreate()-->onStartCommand()-->onDestory())三个过程,Bound Service经历创建-->绑定-->解除绑定-->销毁(onCreate()-->onBind()-->onUnbind()-->onDestory)四个过程,下面将逐一分析每个方法:
     onCreate():
     该方法在Service首次创建的时候调用,位于onStartCommand()和onBind()之前,当Service实例已经存并运行,不会重复调用该方法。
     onStartCommend():
     当前台组件调用startService()用于开启一个服务的时候触发该方法,当Service执行完毕或调用stopService()方法时,Service将会销毁掉。
     onBind():
     当前台组件调用boundService()用于绑定一个服务时触发该方法。
     onUnbind():
     当前台所有绑定该Service的应用程序组件调用unBindService()时,该方法将调用。
     onDestory():
     Service停止或解除绑定时,该方法将调用,Service销毁。

3、Service实例讲解

      以上基本了解了两种Service的原理和生命周期后,接下来讲通过实例来深入分析两种Service的使用方法:

Started Service:

     创建Started Service一般也分两种方法,分别为:

继承Service:

     Service类是所有Services的基类,在继承Service创建自定义的Service时,最好在自定义的Service类中新建一个线程用于执行Service中所有的耗时工作,因为Service实例创建后默认情况下不会新建线程(依然在主线程中运行)。因此,为了避免在Service中执行较为繁重的工作而导致的前台Activity阻塞的情况发生,新创建线程用于执行耗时任务是较为明智的选择。下面我们使用一个实例来熟悉一下继承Service来Started Serivce的使用方法,主要源代码如下:
MyServiceDemo.java:

[java] view plaincopy
  1. import android.app.Service;  
  2. import android.content.Intent;  
  3. import android.os.Binder;  
  4. import android.os.IBinder;  
  5. import android.util.Log;  
  6. import android.widget.Toast;  
  7.   
  8. public class MyServiceDemo extends Service {  
  9.   
  10.     private static final String TAG = "ygh";  
  11.       
  12.     @Override  
  13.     public IBinder onBind(Intent arg0) {  
  14.         // TODO Auto-generated method stub  
  15.         Log.i(TAG, "onBind()");  
  16.         return new MyBinder();  
  17.     }  
  18.     @Override  
  19.     public void onCreate() {  
  20.         // TODO Auto-generated method stub  
  21.         Log.i(TAG, "onCreate()");  
  22.         super.onCreate();  
  23.     }     
  24.     @Override  
  25.     public void onStart(Intent intent, int startId) {  
  26.         // TODO Auto-generated method stub  
  27.         Log.i(TAG, "onStart()");  
  28.         super.onStart(intent, startId);  
  29.     }  
  30.     @Override  
  31.     public int onStartCommand(Intent intent, int flags, int startId) {  
  32.         // TODO Auto-generated method stub  
  33.         Log.i(TAG, "onStartCommand()");  
  34.         return super.onStartCommand(intent, flags, startId);  
  35.     }  
  36.     @Override  
  37.     public void onRebind(Intent intent) {  
  38.         // TODO Auto-generated method stub  
  39.         Log.i(TAG, "onRebind()");  
  40.         super.onRebind(intent);  
  41.     }  
  42.     @Override  
  43.     public boolean onUnbind(Intent intent) {  
  44.         // TODO Auto-generated method stub  
  45.         Log.i(TAG, "onUnbind()");  
  46.         return super.onUnbind(intent);  
  47.     }  
  48.     @Override  
  49.     public void onDestroy() {  
  50.         // TODO Auto-generated method stub  
  51.         Log.i(TAG, "onDestroy()");  
  52.         super.onDestroy();  
  53.     }  
  54.     //自定义MyBinder类,继承Binder类  
  55.     public class MyBinder extends Binder  
  56.     {  
  57.         public void getString(String str)  
  58.         {  
  59.             Toast.makeText(getApplicationContext(), "Hello, "+ str, Toast.LENGTH_LONG).show();    
  60.         }  
  61.     }  
  62. }  
      主界面MainActivity.java的主要源代码如下(布局文件和控件的声明代码省略):

[java] view plaincopy
  1. //开启Service  
  2.     public void startServiceMethod()  
  3.     {  
  4.         Intent mIntent = new Intent(this,MyServiceDemo.class);  
  5.           
  6.         this.startService(mIntent);  
  7.     }  
  8.     //停止Service  
  9.     public void stopServiceMethod()  
  10.     {  
  11.         Intent mIntent = new Intent(this,MyServiceDemo.class);  
  12.         this.stopService(mIntent);  
  13.     }  

     我们通过点击MainActivity的布局界面中这两个按钮来做试验:


     如上图示,我们点击“Start Service”按钮调用startServiceMethod()方法开启服务,点击“Stop Service”按钮调用stopServiceMethod()方法停止服务。首先,我们点击“Start Service”,打印的Log信息如下:


      开启服务,首先调用onCreate()、onStartCommand()和onStart()方法,服务在后台中开始运行,然后再多次(三次)重复点击”Start Service“按钮,打印的Log信息如下:


      由上图可知,onCreate()方法只会在Service首次实例化的时候调用,当Service对象存在的情况下,调用startService()方法只会重复调用onStartCommand()和onStart()方法。接下来点击“Stop Service”,打印的Log信息如下:


      停止服务,调用onDestory()方法,服务停止并销毁。
      在Service的生命周期中,有onStartCommand()方法,它在系统调用onCreate()之后调用,onStartCommand()方法返回一个int类型的值,返回值一般分为以下三种:
START_NOT_STICKY
     Service在调用onStartCommand()后被Kill掉,系统不会自动重启服务,除非有其它等待的Intent启动服务。当不需要重启服务或者是有些应用可以非常容易重启未完成的工作时,使用该参数是最佳的选择。
START_STICKY
     当Service在调用onStartCommand()后被Kill掉,系统会自动重启服务并调用onStartCommand()方法,但不传递最后的Intent对象,系统若调用onStartCommand()方法,返回的Intent对象的值为null。除非有其它等待的Intent启动服务,Intent才会被传递。该参数比较合适音频、视频等媒体播放情况,不执行命令,但无限运行并等待。
START_REDELIVER_INTENT
     当Service在调用onStartCommand()后被Kill掉,系统会自动重启服务并调用onStartCommand()方法,同时也传递最后的Intent对象。该参数比较适合那些较为活跃的需立即重启服务和返回各种Intent的工作,如下载文件。

继承IntentService:

     该类是Service的子类,继承IntentService与直接继承Service执行任务的区别是前者自动创建了一个工作线程用于处理所有的开启请求,若用户不需要执行多任务的请求时,使用IntentService方法是最佳的的选择。该类中有一个onHandleIntent()方法,使用时子类需重写这个方法,所有的后台工作都将在该方法中执行。由于大多数的Started Service操作都不需要使用多任务请求(实际上多任务请求比较危险),因此使用IntentService是最好的选择。那么相比Service,使用IntentService多做了哪些工作呢?如下罗列了几条:
<1>在主线程之外,创建了一个默认的工作线程(work Thread)用于将所有的意图操作传递给onstartCommend()中。
<2>默认创建了一个工作队列(work queue)用于将意图一一传递到onHandleIntent()方法中,因此用户不需要担心多线程的问题。
<3>当所有的任务请求都结束后会自动停止服务(Stop Service),因此用户不需要调用stopSelf()方法。
<4>提供默认的onBind()实现方法,返回值为null。
<5>提供默认的onStartCommand()实现,将所有的意图请求传送到工作队列中然后再一一传递到onHandleIntent()方法实现中。
总的来讲,IntentService是在Service基础上的封装,使用IntentService时,它默认提供了一个工作线程和用于管理消息任务的工作队列,工作队列在Looper的管理下进行消息的接收和分发,然后再通过继承了Handler的ServiceHandler来传递消息给工作队列和接收工作队列分发出来的消息(其涉及到了多线程的异步任务方面的知识,具体的可以学习《Android多线程和异步任务之Handler》)。下面我们来分析一下IntentService的源码,具体了解IntentService的工作原理和机制,源码如下:

[java] view plaincopy
  1. public abstract class IntentService extends Service {  
  2.     private volatile Looper mServiceLooper;  
  3.     private volatile ServiceHandler mServiceHandler ;  
  4.     private String mName;  
  5.     private boolean mRedelivery ;  
  6.   
  7.     private final class ServiceHandler extends Handler {  
  8.         public ServiceHandler(Looper looper) {  
  9.             super(looper);  
  10.         }  
  11.   
  12.         @Override  
  13.         public void handleMessage(Message msg) {  
  14.             onHandleIntent((Intent)msg.obj);  
  15.             stopSelf(msg.arg1);  
  16.         }  
  17.     }  
  18.   
  19.     /** 
  20.      * Creates an IntentService.  Invoked by your subclass's constructor. 
  21.      * 
  22.      * @param name Used to name the worker thread, important only for debugging. 
  23.      */  
  24.     public IntentService(String name) {  
  25.         super();  
  26.         mName = name;  
  27.     }  
  28.   
  29.     /** 
  30.      * Sets intent redelivery preferences.  Usually called from the constructor 
  31.      * with your preferred semantics. 
  32.      * 
  33.      * <p>If enabled is true, 
  34.      * {@link #onStartCommand(Intent, int, int)} will return 
  35.      * {@link Service#START_REDELIVER_INTENT} , so if this process dies before 
  36.      * {@link #onHandleIntent(Intent)} returns, the process will be restarted 
  37.      * and the intent redelivered.  If multiple Intents have been sent, only 
  38.      * the most recent one is guaranteed to be redelivered. 
  39.      * 
  40.      * <p>If enabled is false (the default), 
  41.      * {@link #onStartCommand(Intent, int, int)} will return 
  42.      * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent 
  43.      * dies along with it. 
  44.      */  
  45.     public void setIntentRedelivery(boolean enabled) {  
  46.         mRedelivery = enabled;  
  47.     }  
  48.   
  49.     @Override  
  50.     public void onCreate() {  
  51.         // TODO: It would be nice to have an option to hold a partial wakelock  
  52.         // during processing, and to have a static startService(Context, Intent)  
  53.         // method that would launch the service & hand off a wakelock.  
  54.   
  55.         super.onCreate();  
  56.         HandlerThread thread = new HandlerThread("IntentService[" + mName + "]" );  
  57.         thread.start();  
  58.   
  59.         mServiceLooper = thread.getLooper();  
  60.         mServiceHandler = new ServiceHandler(mServiceLooper );  
  61.     }  
  62.   
  63.     @Override  
  64.     public void onStart(Intent intent, int startId) {  
  65.         Message msg = mServiceHandler.obtainMessage();  
  66.         msg.arg1 = startId;  
  67.         msg.obj = intent;  
  68.         mServiceHandler.sendMessage(msg);  
  69.     }  
  70.   
  71.     @Override  
  72.     public int onStartCommand(Intent intent, int flags, int startId) {  
  73.         onStart(intent, startId);  
  74.         return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY ;  
  75.     }  
  76.   
  77.     @Override  
  78.     public void onDestroy() {  
  79.         mServiceLooper.quit();  
  80.     }  
  81.   
  82.     @Override  
  83.     public IBinder onBind(Intent intent) {  
  84.         return null ;  
  85.     }  
  86.   
  87.     /** 
  88.      * This method is invoked on the worker thread with a request to process. 
  89.      * Only one Intent is processed at a time, but the processing happens on a 
  90.      * worker thread that runs independently from other application logic. 
  91.      * So, if this code takes a long time, it will hold up other requests to 
  92.      * the same IntentService, but it will not hold up anything else. 
  93.      * 
  94.      * @param intent The value passed to {@link 
  95.      *               android.content.Context#startService(Intent)}. 
  96.      */  
  97.     protected abstract void onHandleIntent(Intent intent);  
  98. }  
      IntentService按照onCreate()-->onStartCommand()-->onStart()-->onDestory()的生命周期调用的,因此分析源码,首先我们先从onCreate()方法看起:
[java] view plaincopy
  1. public void onCreate() {  
  2.     // TODO: It would be nice to have an option to hold a partial wakelock  
  3.     // during processing, and to have a static startService(Context, Intent)  
  4.     // method that would launch the service & hand off a wakelock.  
  5.   
  6.     super.onCreate();  
  7.     HandlerThread thread = new HandlerThread("IntentService[" + mName + "]" );  
  8.     thread.start();  
  9.   
  10.     mServiceLooper = thread.getLooper();  
  11.     mServiceHandler = new ServiceHandler(mServiceLooper );  
  12. }  
      在onCreate()方法中,首先实例化一个HandlerThread对象,该对象负责创建了一个新的线程,然后开启线程,开启线程后通过thread.getLooper()方法创建MessageQueue(消息队列)和得到Looper循环管理对象。最后,再实例化一个ServiceHandler对象,并将得到的Looper对象作为参数传递给ServiceHandler。我们继续跟踪,再来分析一下ServiceHandler类:
[java] view plaincopy
  1. private final class ServiceHandler extends Handler {  
  2.     public ServiceHandler(Looper looper) {  
  3.         super(looper);  
  4.     }  
  5.   
  6.     @Override  
  7.     public void handleMessage(Message msg) {  
  8.         onHandleIntent((Intent)msg.obj);  
  9.         stopSelf(msg.arg1);  
  10.     }  
  11. }  
      分析源码可知,ServiceHandler继承至Handler,主要作用是将子线程中的Message发送到主线程中并在子主线程中执行UI更新的操作,简单来讲就是负责线程间的通信。ServiceHandler通过构造方法,将在onCreate()方法中得到的Looper对象传递到ServiceHandler类中并继续传递给父类。一般来讲Handler类中的handleMessage(Message msg)方法主要是接收子线传递过来的Message对象并做些相关的UI更新操作,而在ServiceHandler中是直接调用了onHandleIntent(Intent mIntent)抽象方法,并且该方法执行完成后调用stopSelf()方法自动停止。onCreate()方法之后会调用onStartCommand()方法,看源码:
[java] view plaincopy
  1. @Override  
  2. public void onStart(Intent intent, int startId) {  
  3.     //得到Message对象  
  4.     Message msg = mServiceHandler.obtainMessage();  
  5.     //为Message设置标识  
  6.     msg. arg1 = startId;  
  7.     //将从前台传递过来的intent包装成消息对象  
  8.     msg. obj = intent;  
  9.     //将消息对象传递到子线程中,在子线程中处理相关操作  
  10.     mServiceHandler.sendMessage(msg);  
  11. }  
  12.   
  13. @Override  
  14. public int onStartCommand(Intent intent, int flags, int startId) {  
  15.     onStart(intent, startId);  
  16.     return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY ;  
  17. }  
      触发onStartCommand()方法,直接调用onStart()方法,在该方法中,首先通过ServiceHandler对象得到一个Message的对象,然后使用msg.obj=intent将从前台传递过来的intent包装成消息对象,然后调用mServiceHandler.sendMessage(msg)将消息对象传递到子线程的消息队列中。
再看ServiceHandler类中的handleMessage(Message msg)方法,该方法是接收传递过来的Message对象,并调用抽象方法onHandleIntent(Intent Service)方法,用户使用IntentService类时需继承IntentService并重写onHandleIntent(Intent service)方法,将主要耗时操作方法该方法中进行(注意,该操作是在新开的子线程中进行的)。操作执行完成自动调用stopSelf(msg.arg1)方法,根据标识将Service停止。

[java] view plaincopy
  1. @Override  
  2. public void handleMessage(Message msg) {  
  3.  //调用onHandleIntent()抽象方法  
  4.  onHandleIntent((Intent)msg. obj);  
  5.  stopSelf(msg. arg1);  
  6. }  
      执行完所有的操作后,系统也会调用onDestory()方法将Looper停止循环并退出:
[java] view plaincopy
  1. @Override  
  2.     public void onDestroy() {  
  3.     //停止Looper循环并退出  
  4.         mServiceLooper.quit();  
  5.     }  
      下面我们将通过一个实例来进一步了解IntentService的使用方法,我们将模拟一个场景,就是在IntentService执行一项较为耗时的任务,然后同时也在主线程中执行一项耗时任务,观察两项任务是顺序执行还是相互交替执行,若是顺序执行那么可以确定使在IntentService中并没有开启一个新的线程来执行耗时任务,而如果是相互交替执行的话那就说明IntentService新开启了一个新的子线程与主线程相互抢占CPU资源,两个线程相互交替执行。首先自定义一个MyIntentServiceExecuteDemo.java,继承IntentService类,然后重写onHandleIntent()方法,然后在该方法中利用线程的睡眠来模拟一个耗时操作。话少说,直接上源码:
[java] view plaincopy
  1. import android.app.IntentService;  
  2. import android.content.Intent;  
  3. import android.util.Log;  
  4.   
  5. public class MyIntentServiceExecuteDemo extends IntentService {  
  6.       
  7.     private static final String TAG = "ygh";  
  8.   
  9.     public MyIntentServiceExecuteDemo() {  
  10.           super("MyIntentServiceExecuteDemo" );  
  11.           // TODO Auto-generated constructor stub  
  12.     }  
  13.     @Override  
  14.     protected void onHandleIntent(Intent arg0) {  
  15.           // TODO Auto-generated method stub  
  16.           //利用线程睡眠来模拟耗时操作  
  17.           for(int   i = 0; i < 50;i++)  
  18.          {  
  19.              Log. i(TAG, "IntentService中子线程执行。。。。。。。" + (i + 1));  
  20.               try {  
  21.                  Thread. sleep(500);  
  22.              } catch (InterruptedException e) {  
  23.                   // TODO Auto-generated catch block  
  24.                  e.printStackTrace();  
  25.              }  
  26.          }  
  27.     }  
  28. }  
     然后主界面MainActivity.java中的主要代码为(布局文件和控件的声明代码省略):
[java] view plaincopy
  1. //使用Intentservice执行耗时任务  
  2.     public void intentServiceExecute()  
  3.     {  
  4.           //创建Intent对象  
  5.          Intent mIntent = new Intent(this,MyIntentServiceExecuteDemo. class);  
  6.           //开启IntentService  
  7.           this.startService(mIntent);  
  8.     }  
  9.     //开启主线程中的耗时操作(这里用线程睡眠来模拟)  
  10.     public void startMainExecute()  
  11.     {  
  12.          waitForMinutes();  
  13.     }  
  14.     //耗时操作方法  
  15.     public void waitForMinutes()  
  16.     {     
  17.           for(int   i = 0; i < 50;i++)  
  18.          {  
  19.              Log. i(TAG, "主线程中执行。。。。。。。" + (i + 1));  
  20.               try {  
  21.                  Thread. sleep(500);  
  22.              } catch (InterruptedException e) {  
  23.                   // TODO Auto-generated catch block  
  24.                  e.printStackTrace();  
  25.              }  
  26.          }  
  27.     }  

      我们通过点击MainActivity的布局界面中这两个按钮来做试验:


      首先点击“Use IntentService Execute”按钮,调用了intentServiceExecute()方法开启MyIntentServiceExecuteDemo服务,然后再迅速点击"Start MainThread Execute"按钮,调用了startMainExecute()方法,模拟在主线程中执行一项任务。最后观察Log信息,如下图示:


     如上图我们可知,当开启IntentService服务后再运行主线程中的任务,IntentService中的任务和MainThread中的任务相互交替执行,由此我们可以断定IntentService新创建了一个线程用于执行后台任务。
前面我们讲了IntentService是在Service基础上的封装,那么我下面使用Service来完成上面同样的实验,看看有什么区别,主要代码如下:

MyServiceExecuteDemo.java:

[java] view plaincopy
  1. import android.app.Service;  
  2. import android.content.Intent;  
  3. import android.os.Handler;  
  4. import android.os.HandlerThread;  
  5. import android.os.IBinder;  
  6. import android.os.Looper;  
  7. import android.os.Message;  
  8. import android.util.Log;  
  9. import android.widget.Toast;  
  10.   
  11. public class MyServiceExecuteDemo extends Service {  
  12.      private Looper mServiceLooper;  
  13.       private ServiceHandler mServiceHandler;  
  14.   
  15.       private static final String TAG = "ygh";  
  16.       // Handler that receives messages from the thread  
  17.       private final class ServiceHandler extends Handler {  
  18.           public ServiceHandler(Looper looper) {  
  19.               super(looper);  
  20.           }  
  21.           @Override  
  22.           public void handleMessage(Message msg) {  
  23.               // Normally we would do some work here, like download a file.  
  24.               // For our sample, we just sleep for 5 seconds.  
  25.           for(int   i = 0; i < 50;i++)  
  26.                  {  
  27.                       Log. i(TAG, "Service中子线程执行。。。。。。。" + (i + 1));  
  28.                        try {  
  29.                           Thread. sleep(500);  
  30.                       } catch (InterruptedException e) {  
  31.                            // TODO Auto-generated catch block  
  32.                           e.printStackTrace();  
  33.                       }  
  34.                  }  
  35.               // Stop the service using the startId, so that we don't stop  
  36.               // the service in the middle of handling another job  
  37.               stopSelf(msg. arg1);  
  38.           }  
  39.       }  
  40.   
  41.       @Override  
  42.       public void onCreate() {  
  43.         // Start up the thread running the service.  Note that we create a  
  44.         // separate thread because the service normally runs in the process's  
  45.         // main thread, which we don't want to block.  We also make it  
  46.         // background priority so CPU-intensive work will not disrupt our UI.  
  47.         HandlerThread thread = new HandlerThread("ServiceStartArguments" );  
  48.         thread.start();  
  49.         // Get the HandlerThread's Looper and use it for our Handler  
  50.         mServiceLooper = thread.getLooper();  
  51.         mServiceHandler = new ServiceHandler(mServiceLooper );  
  52.       }  
  53.   
  54.       @Override  
  55.       public int onStartCommand(Intent intent, int flags, int startId) {  
  56.           Toast. makeText(this"service starting", Toast.LENGTH_SHORT ).show();  
  57.   
  58.           // For each start request, send a message to start a job and deliver the  
  59.           // start ID so we know which request we're stopping when we finish the job  
  60.           Message msg = mServiceHandler.obtainMessage();  
  61.           msg. arg1 = startId;  
  62.           mServiceHandler.sendMessage(msg);  
  63.            
  64.           // If we get killed, after returning from here, restart  
  65.           return START_STICKY ;  
  66.       }  
  67.   
  68.       @Override  
  69.       public IBinder onBind(Intent intent) {  
  70.           // We don't provide binding, so return null  
  71.           return null;  
  72.       }  
  73.        
  74.       @Override  
  75.       public void onDestroy() {  
  76.         Toast. makeText(this"service done", Toast.LENGTH_SHORT ).show();  
  77.       }  
  78. }  
      然后主界面MainActivity.java中的主要代码为(布局文件和控件的声明代码省略):
[java] view plaincopy
  1. //使用IntentService执行耗时任务  
  2. public void serviceExecute()  
  3. {  
  4.       //创建Intent对象  
  5.      Intent mIntent = new Intent(this,MyServiceExecuteDemo.class);  
  6.       //开启IntentService  
  7.       this.startService(mIntent);  
  8.        
  9. }  
  10. //开启主线程中的耗时操作(这里用线程睡眠来模拟)  
  11. public void startMainExecute()  
  12. {  
  13.      waitForMinutes();  
  14. }  
  15. //耗时操作方法  
  16. public void waitForMinutes()  
  17. {     
  18.       for(int   i = 0; i < 50;i++)  
  19.      {  
  20.          Log. i(TAG, "主线程中执行。。。。。。。" + (i + 1));  
  21.           try {  
  22.              Thread. sleep(500);  
  23.          } catch (InterruptedException e) {  
  24.               // TODO Auto-generated catch block  
  25.              e.printStackTrace();  
  26.          }  
  27.      }  
  28. }  

      我们通过点击MainActivity的布局界面中这两个按钮来做试验:


      首先点击“Use Service Execute”按钮,调用了serviceExecute()方法开启MyServiceExecuteDemo服务,然后再迅速点击"Start MainThread Execute"按钮,调用了startMainExecute()方法,模拟在主线程中执行一项任务。最后观察Log信息,如下图示:


如上图我们发现执行的效果与使用IntentService执行的效果是一样的。
小结:通过以上的学习和实验,我们可知Service默认是依附在主线程中运行的,所以对于在Service中执行一些较为耗时的操作,我们建议手动新建线程,将耗时操作放在新建的线程中执行。而Google为了方便开发者,提供了IntentService类以便简化开发复杂度,IntentService其实是在Service基础上加入了Handler + Thread机制并进行了良好的封装。IntentService将前台发送过来的若干个Intent(意图)请求加入到MessageQueue中进行异步任务的管理,然后开启一个线程依次对每个耗时任务进行处理,这样避免了由于主线程阻塞导致ANR的情况发生。不过,当需要在后台开启多个线程执行多项任务的时候,使用IntentService就不适合了,因为IntentService默认只开启了一个线程,所以只有使用Service,在Service中手动开启多个线程用户执行多线程多任务的操作。

Bound Service:

      Bound Service是连接客户端前台和后台的一个接口。Service允许其他的应用程序组件(如Activity)调用bindService()方法进行绑定、发送请求和接收返回值甚至执行一些进程间的通信。Bound Service是与其绑定它的组件是共存亡的,当绑定它的组件销毁时,Bound Service也随之销毁掉。要实现绑定操作并建立通信,首先得实例化一个IBinder,该对象主要作用实现客户端前台和后台的通信。一般创建IBinder对象有三种方法:
继承Binder类
     当Service只需在本应用程序中使用而无需进行跨进程的操纵时,继承Binder类是较好的选择。通过创建一个继承Binder的内部类,并将内部类的实例通过onBind()方法返回给前台客户端,前台客户端接收后台返回来的IBinder对象,通过该对象前台可以调用后台所有的公共方法。
使用Messenger
     当需要在不同进程间工作时,可以考虑使用Messenger来实现不同进程之间的通信,Messenger作为进程间的信使来将信息体(Message)发送到另外的进程或后台。并且,我们可以借助Handler来接收Messenger发送过来的Message并进行相关处理,当然前提是Handler与与Messenger已经建立起关联的。举例来讲,首先我们在Service后台自定义Handler并重写handleMessage()方法,然后在Service后台实例化一个Messenger对象,在实例化Messenger的同时也实例化自定义的Handler并建立两者的关联关系。然后,通过onBinder()方法将Messenger包装成IBinder对象返回给客户端前台,客户端前台接收到Messenger后使用该对象发送相关Message给后台(或另外的进程),后台与Messenger关联的自定义的Handler接收到从前台发送过来的Message,然后进一步做相关的处理。
使用AIDL
     AIDL(Android接口定义语言) 执行的所有工作都是将对象分解成操作系统 所能是别的额信息然后再执行进程间的通信。上面所将的Messenger实现原理其实就是将AIDL作为基础架构的。如上所述,Messenger在单个线程中创建了所有的客户端请求,所以Service一次只能接收一个请求。如果想要Service同时处理多个请求,可是使用AIDL来实现。在这种情况下,Service可以建立多个线程并保证线程安全的。由于篇幅关系,我们将在《使用AIDL实现进程间的通信》的博客中详细讲解AIDL的原理和使用方法。
粗略地分析了创建IBinder对象的三种方法,接下来我们将通过实例来了解前两种方法的实际使用:
继承Binder类实例:
Service类为MyServiceDemo.java,上面介绍Started Service时代码已经罗列出来了,
主界面MainActivity.java的主要源代码如下(布局文件和控件的声明代码省略):

[java] view plaincopy
  1. //绑定Service  
  2. public void boundServiceMethod()  
  3. {  
  4.      Intent mIntent = new Intent(this,MyServiceDemo.class);  
  5.       this.bindService(mIntent, mServiceConnection, BIND_AUTO_CREATE );  
  6.       mIsBound = true ;  
  7. }  
  8. //解除Service绑定  
  9. public void unBoundServiceMethod()  
  10. {    if(mIsBound )  
  11.      {  
  12.           this.unbindService(mServiceConnection );  
  13.          Toast. makeText(getApplicationContext(), "unBound Service" , Toast.LENGTH_LONG).show();  
  14.      }  
  15. }  
  16. //使用MyServiceDemo,实例化ServiceConnection对象,建立Activity与后台Service的连接  
  17. ServiceConnection mServiceConnection = new ServiceConnection() {  
  18.        
  19.       public void onServiceDisconnected(ComponentName name) {  
  20.           // TODO Auto-generated method stub  
  21.          Log. i(TAG, "onServiceDisconnected()");  
  22.      }  
  23.       public void onServiceConnected(ComponentName name, IBinder service) {  
  24.           // TODO Auto-generated method stub       
  25.          Log. i(TAG, "onServiceConnected()");  
  26.           myBinder = (MyServiceDemo.MyBinder)service;  
  27.           myBinder.getString("Mr Li" );  
  28.      }  
  29. };  
      我们通过点击MainActivity的布局界面中这两个按钮来做试验:

      首先点击”Extending Binder Bound Service“按钮将会调用boundServiceMethod()方法绑定Service,后台打印的Log信息为:


      前台显示一个Toast信息为”Hello,Mr Li“:

接下来我们详细跟踪代码来分析:
首先点击绑定按钮后前台调用this.bindService()绑定一个后台Service,
后台依次调用onCreate()、onBind()和onStartCommand()方法,后台创建继承Binder类的自定义类MyBinder,并自定义一个getString(String str)方法,代码如下:
[java] view plaincopy
  1. //自定义MyBinder类,继承Binder类  
  2.     public class MyBinder extends Binder  
  3.     {  
  4.           public void getString(String str)  
  5.          {  
  6.              Toast. makeText(getApplicationContext(), "Hello, "+ str, Toast.LENGTH_LONG).show();    
  7.          }  
  8.     }  
      然后再通过onBind()方法将实例化后的MyBinder对象返回给客户端前台,具体代码如下:
[java] view plaincopy
  1. @Override  
  2.     public IBinder onBind(Intent arg0) {  
  3.           // TODO Auto-generated method stub  
  4.          Log. i(TAG, "onBind()");  
  5.           return new MyBinder();  
  6.     }  
     为了接受Service端传递过来的Ibinder对象,前台需实例化ServiceConnection对象。该对象接收到从Service传递过来的IBinder对象后可以根据IBinder对象调用Service中的所有公共方法,代码如下:
[java] view plaincopy
  1. //使用MyServiceDemo,实例化ServiceConnection对象,建立Activity与后台Service的连接  
  2.     ServiceConnection mServiceConnection = new ServiceConnection() {  
  3.            
  4.           public void onServiceDisconnected(ComponentName name) {  
  5.               // TODO Auto-generated method stub  
  6.              Log. i(TAG, "onServiceDisconnected()");  
  7.          }  
  8.           public void onServiceConnected(ComponentName name, IBinder service) {  
  9.               // TODO Auto-generated method stub       
  10.              Log. i(TAG, "onServiceConnected()");  
  11.               myBinder = (MyServiceDemo.MyBinder)service;  
  12.               myBinder.getString("Mr Li" );  
  13.          }  
  14.     };  
至此,我们便很容易理解为什么点击绑定服务按钮会弹出”Hello,Mr Li“的Toast信息。
     接下来再点击”Extending Binder UnBound Service“按钮,解除服务的绑定,打印的Log信息如下:

客户端前台调用unbindService()方法后,后台Service依次调用onUnbind()和onDestory()方法,Service解除绑定并自动销毁掉。

4、使用Messenger实例:

      Messenger的使用原理前面简单的介绍了下,即借助Handler、IBinder和Messenger对象实现Service后台与客户端前台(或远程)的通信。下面我们即将通过一个实例来更加深入地理解Messenger的原理和使用方法。首先看下使用Messenger的使用步骤:
<1>在Service中实现实现Handler类,用于接收客户端前台发送过来的值;
<2>通过引用Handler来创建一个Messenger对象,这里可以理解为建立Handler与Messenger的关联。
<3>Messenger创建一个IBinder对象,并利用onBind()方法将该IBinder对象发给客户端前台
<4>前台客户端利用后台Service传过来的IBinder对象来实例化Messenger对象,并使用Messenger对象发送Message至后台Service;
<5>后台Service的Handler接收从客户端前台发送过来的Message,并将在HandleMessage()方法中执行相关操作。
自定义MessengerService类端的源代码:
[java] view plaincopy
  1. /* 
  2.  * Copyright (C) 2010 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package com.example.android.apis.app;  
  18.   
  19. import android.app.Notification;  
  20. import android.app.NotificationManager;  
  21. import android.app.PendingIntent;  
  22. import android.app.Service;  
  23. import android.content.Intent;  
  24. import android.os.Binder;  
  25. import android.os.Handler;  
  26. import android.os.IBinder;  
  27. import android.os.Message;  
  28. import android.os.Messenger;  
  29. import android.os.RemoteException;  
  30. import android.util.Log;  
  31. import android.widget.Toast;  
  32.   
  33. import java.util.ArrayList;  
  34.   
  35. // Need the following import to get access to the app resources, since this  
  36. // class is in a sub-package.  
  37. import com.example.android.apis.R;  
  38. import com.example.android.apis.app.RemoteService.Controller;  
  39.   
  40. /** 
  41.  * This is an example of implementing an application service that uses the 
  42.  * {@link Messenger} class for communicating with clients.  This allows for 
  43.  * remote interaction with a service, without needing to define an AIDL 
  44.  * interface. 
  45.  * 
  46.  * <p>Notice the use of the {@link NotificationManager} when interesting things 
  47.  * happen in the service.  This is generally how background services should 
  48.  * interact with the user, rather than doing something more disruptive such as 
  49.  * calling startActivity(). 
  50.  */  
  51.   
  52. public class MessengerService extends Service {  
  53.     /** For showing and hiding our notification. */  
  54.     NotificationManager mNM;  
  55.     /** Keeps track of all current registered clients. */  
  56.     ArrayList<Messenger> mClients = new ArrayList<Messenger>();  
  57.     /** Holds last value set by a client. */  
  58.     int mValue = 0;  
  59.      
  60.     /** 
  61.      * Command to the service to register a client, receiving callbacks 
  62.      * from the service.  The Message's replyTo field must be a Messenger of 
  63.      * the client where callbacks should be sent. 
  64.      */  
  65.     static final int MSG_REGISTER_CLIENT = 1;  
  66.      
  67.     /** 
  68.      * Command to the service to unregister a client, ot stop receiving callbacks 
  69.      * from the service.  The Message's replyTo field must be a Messenger of 
  70.      * the client as previously given with MSG_REGISTER_CLIENT. 
  71.      */  
  72.     static final int MSG_UNREGISTER_CLIENT = 2;  
  73.      
  74.     /** 
  75.      * Command to service to set a new value.  This can be sent to the 
  76.      * service to supply a new value, and will be sent by the service to 
  77.      * any registered clients with the new value. 
  78.      */  
  79.     static final int MSG_SET_VALUE = 3;  
  80.      
  81.     /** 
  82.      * Handler of incoming messages from clients. 
  83.      * <1>创建一个继承Handler的子类IncomingHandler,用于接收客户端前台发送过来的Message 
  84.      */  
  85.     class IncomingHandler extends Handler {  
  86.      
  87.     //<5>后台Service的Handler接收从客户端前台发送过来的Message,并将在HandleMessage()方法中执行相关操作。  
  88.         @Override  
  89.         public void handleMessage(Message msg) {  
  90.             switch (msg.what ) {  
  91.                 case MSG_REGISTER_CLIENT :  
  92.                     mClients.add(msg.replyTo );  
  93.                     break;  
  94.                 case MSG_UNREGISTER_CLIENT :  
  95.                     mClients.remove(msg.replyTo );  
  96.                     break;  
  97.                 case MSG_SET_VALUE :  
  98.                     mValue = msg.arg1 ;  
  99.                      
  100.                     for (int i=mClients.size()-1; i>=0; i--) {  
  101.                         try {  
  102.                           //将客户端前台发送过来的哈希值再发送到前台打印  
  103.                             mClients.get(i).send(Message.obtain( null,  
  104.                                     MSG_SET_VALUE, mValue, 0));  
  105.                         } catch (RemoteException e) {  
  106.                             // The client is dead.  Remove it from the list;  
  107.                             // we are going through the list from back to front  
  108.                             // so this is safe to do inside the loop.  
  109.                             mClients.remove(i);  
  110.                         }  
  111.                     }  
  112.                     break;  
  113.                 default:  
  114.                     super.handleMessage(msg);  
  115.             }  
  116.         }  
  117.     }  
  118.      
  119.     /** 
  120.      * Target we publish for clients to send messages to IncomingHandler. 
  121.      * <2>通过引用Handler来创建一个Messenger对象,这里可以理解为建立Handler与Messenger的关联。 
  122.      */  
  123.     final Messenger mMessenger = new Messenger(new IncomingHandler());  
  124.      
  125.     @Override  
  126.     public void onCreate() {  
  127.         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  128.          
  129.         // Display a notification about us starting.  
  130.         showNotification();  
  131.     }  
  132.   
  133.     @Override  
  134.     public void onDestroy() {  
  135.         // Cancel the persistent notification.  
  136.         mNM.cancel(R.string.remote_service_started);  
  137.         // Tell the user we stopped.  
  138.         Toast. makeText(this, R.string.remote_service_stopped , Toast.LENGTH_SHORT ).show();  
  139.     }  
  140.      
  141.     /** 
  142.      * When binding to the service, we return an interface to our messenger 
  143.      * for sending messages to the service. 
  144.      * <3>Messenger创建一个IBinder对象,并利用onBind()方法将该IBinder对象发给客户端前台 
  145.      */  
  146.     @Override  
  147.     public IBinder onBind(Intent intent) {  
  148.         return mMessenger .getBinder();  
  149.     }  
  150.   
  151.     /** 
  152.      * Show a notification while this service is running. 
  153.      * 当Service运行的时候显示一个消息通知 
  154.      */  
  155.     private void showNotification() {  
  156.         // In this sample, we'll use the same text for the ticker and the expanded notification  
  157.         CharSequence text = getText(R.string.remote_service_started );  
  158.   
  159.         // Set the icon, scrolling text and timestamp  
  160.         Notification notification = new Notification(R.drawable. stat_sample, text,  
  161.                 System. currentTimeMillis());  
  162.   
  163.         // The PendingIntent to launch our activity if the user selects this notification  
  164.         PendingIntent contentIntent = PendingIntent.getActivity(this0,  
  165.                 new Intent(this, Controller.class), 0);  
  166.   
  167.         // Set the info for the views that show in the notification panel.  
  168.         notification. setLatestEventInfo( this, getText(R.string.remote_service_label ),  
  169.                        text, contentIntent) ;  
  170.   
  171.         // Send the notification.  
  172.         // We use a string id because it is a unique number.  We use it later to cancel.  
  173.         mNM.notify(R.string.remote_service_started, notification);  
  174.     }  
  175. }  
      然后主界面MainActivity.java中的主要代码为(布局文件和控件的声明代码省略):
[java] view plaincopy
  1. //创建一个继承Handler的子类IncomingHandler,用于接收Service后台发送过来的Message(本对象的哈希值)  
  2.      class IncomingHandler extends Handler {  
  3.          @Override  
  4.          public void handleMessage(Message msg) {  
  5.              switch (msg.what ) {  
  6.                  case MessengerService.MSG_SET_VALUE :  
  7.                      mCallbackText.setText("Received from service: " + msg.arg1);  
  8.                      break;  
  9.                  default:  
  10.                      super.handleMessage(msg);  
  11.              }  
  12.          }  
  13.      }  
  14.      //实例化Messenger和IncomingHandler对象并建立两者之间的关联  
  15.      final Messenger mMessenger = new Messenger(new IncomingHandler());  
  16.       
  17.      //绑定Service  
  18.     public void messengerBoundServiceMethod()  
  19.     {  
  20.           this.bindService(new Intent(MainActivity.this , MessengerService.class), mServiceConnection2, Context.BIND_AUTO_CREATE);  
  21.           mIsBound = true ;  
  22.           mCallbackText.setText("Binding." );  
  23.     }  
  24.     //解除Service绑定  
  25.     public void messengerUnBoundServiceMethod()  
  26.     {  
  27.           if (mIsBound ) {  
  28.              // If we have received the service, and hence registered with  
  29.              // it, then now is the time to unregister.  
  30.              if (mService != null) {  
  31.                  try {  
  32.                   //将客户端前台的Messenger发送至Service后台,后台将持有对应该Messenger的对象删除  
  33.                      Message msg = Message.obtain(null,  
  34.                              MessengerService.MSG_UNREGISTER_CLIENT );  
  35.                      msg. replyTo = mMessenger ;  
  36.                      mService.send(msg);  
  37.                  } catch (RemoteException e) {  
  38.                      // There is nothing special we need to do if the service  
  39.                      // has crashed.  
  40.                  }  
  41.              }  
  42.               
  43.              // Detach our existing connection.  
  44.              unbindService( mServiceConnection2);  
  45.              mIsBound = false ;  
  46.             mCallbackText.setText("Unbinding." );  
  47.           }  
  48.     }  
  49. //使用MessengerService,实例化ServiceConnection对象,建立Activity和后台Service的连接  
  50.     ServiceConnection mServiceConnection2 = new ServiceConnection() {  
  51.            
  52.           public void onServiceDisconnected(ComponentName name) {  
  53.               // TODO Auto-generated method stub  
  54.               // This is called when the connection with the service has been  
  55.             // unexpectedly disconnected -- that is, its process crashed.  
  56.             mService = null ;  
  57.             mCallbackText.setText("Disconnected." );  
  58.   
  59.             // As part of the sample, tell the user what happened.  
  60.             Toast. makeText(MainActivity.this"remote_service_disconnected" ,  
  61.                     Toast. LENGTH_SHORT).show();  
  62.                
  63.          }  
  64.            
  65.           public void onServiceConnected(ComponentName name, IBinder service) {  
  66.               // TODO Auto-generated method stub  
  67.               //得到从Service发送过来的IBinder对象并初始化Messenger  
  68.               mService = new Messenger(service);  
  69.                
  70.               mCallbackText.setText("Attached." );  
  71.               // We want to monitor the service for as long as we are  
  72.             // connected to it.  
  73.             try {  
  74.               //将客户端前台的Messenger对象发送至Service,并通知后台的Messenger集合持有该对象  
  75.                 Message msg = Message. obtain(null,  
  76.                         MessengerService.MSG_REGISTER_CLIENT );  
  77.                 msg. replyTo = mMessenger ;  
  78.                 //发送Message  
  79.                 mService.send(msg);  
  80.                  
  81.                 // Give it some value as an example.  
  82.                 //将本对象的哈希值发送给Service后台  
  83.                 msg = Message. obtain(null,  
  84.                         MessengerService.MSG_SET_VALUE , this.hashCode(), 0);  
  85.                 mService.send(msg);  
  86.             } catch (RemoteException e) {  
  87.                 // In this case the service has crashed before we could even  
  88.                 // do anything with it; we can count on soon being  
  89.                 // disconnected (and then reconnected if it can be restarted)  
  90.                 // so there is no need to do anything here.  
  91.             }  
  92.              
  93.             // As part of the sample, tell the user what happened.  
  94.             Toast.makeText(MainActivity.this"remote_service_connected",  
  95.                     Toast. LENGTH_SHORT).show();  
  96.          }  
  97.     };  
     我们通过点击MainActivity的布局界面中这两个按钮来做试验:

     首先点击“Use Messenegr Bound Service ”按钮调用messengerBoundServiceMethod()方法绑定Service,前台显示的信息如图:
      显示一个Toast信息、一个通知和本对象的哈希值(从Service后台发送过来的)。
我们来追踪一下代码,点击开始绑定Service后,运行到后台MessengerService中,然后后台使用onBind()方法将后台的创建的Messenger对象所得到的IBinder对象发送到客户端前台:
[java] view plaincopy
  1. @Override  
  2.    public IBinder onBind(Intent intent) {  
  3.    //使用Messenger得到IBinder对象并返回给客户端前台  
  4.        return mMessenger .getBinder();  
  5.    }  
然后客户端前台ServiceConnection的onServiceConnection()方法接收从Service后台发送过来的IBinder对象,并通过该对象初始化Messenger对象,至此,客户端前台便持有了Service后台的Messenger对象,那么前台就可以借此对象来建立与Service后台的通信。
[java] view plaincopy
  1. public void onServiceConnected(ComponentName name, IBinder service) {  
  2.               // TODO Auto-generated method stub  
  3.               //得到从Service发送过来的IBinder对象并初始化成Messenger  
  4.               mService = new Messenger(service);  
  5.                
  6.               mCallbackText .setText("Attached." );  
  7.               // We want to monitor the service for as long as we are  
  8.             // connected to it.  
  9.             try {  
  10.               //将客户端前台的Messenger对象发送至Service,并通知后台的Messenger集合持有该对象(目的是建立前台和后台的双向通信)  
  11.                 Message msg = Message. obtain( null,  
  12.                         MessengerService.MSG_REGISTER_CLIENT );  
  13.                 msg. replyTo = mMessenger ;  
  14.                 //发送Message  
  15.                 mService .send(msg);  
  16.                  
  17.                 // Give it some value as an example.  
  18.                 //将本对象的哈希值发送给Service后台  
  19.                 msg = Message. obtain( null,  
  20.                         MessengerService.MSG_SET_VALUE , this.hashCode(), 0);  
  21.                 mService .send(msg);  
  22.             } catch (RemoteException e) {  
  23.                 // In this case the service has crashed before we could even  
  24.                 // do anything with it; we can count on soon being  
  25.                 // disconnected (and then reconnected if it can be restarted)  
  26.                 // so there is no need to do anything here.  
  27.             }  
  28.             // As part of the sample, tell the user what happened.  
  29.             Toast.makeText(MainActivity. this"remote_service_connected" ,  
  30.                     Toast. LENGTH_SHORT ).show();  
  31.          }  
不过这里只建立了从前台客户端向Service后台发送信息的单向通信,若要建立双向通信,那么同样也要在前台客户端创建自定义的Handler类和Messenger对象,然后将前台的Messenger对象作为Message发送给Service后台,使Service后台也持有客户端前台的Messenger对象,那么至此就建立了前台和后台的通信了,具体代码如下:
<1>客户端前台建立自定义的Handler,用于接收Service后台传递过来的Message:
[java] view plaincopy
  1. //创建一个继承Handler的子类IncomingHandler,用于接收Service后台发送过来的Message(本对象的哈希值)  
  2.      class IncomingHandler extends Handler {  
  3.          @Override  
  4.          public void handleMessage(Message msg) {  
  5.              switch (msg.what ) {  
  6.                  case MessengerService.MSG_SET_VALUE :  
  7.                      mCallbackText.setText("Received from service: " + msg.arg1);  
  8.                      break;  
  9.                  default:  
  10.                      super.handleMessage(msg);  
  11.              }  
  12.          }  
  13.      }  
<2>客户端前台根据自定义Handler来实例化自己的Messenger对象:
[java] view plaincopy
  1. //实例化Messenger和IncomingHandler对象并建立两者之间的关联  
  2.      final Messenger mMessenger = new Messenger(new IncomingHandler());  
<3>将客户端前台的Messenger对象作为Message发送给Service后台:
[java] view plaincopy
  1. //将客户端前台的Messenger对象发送至Service,并通知后台的Messenger集合持有该对象  
  2.      Message msg = Message.obtain(null,  
  3.      MessengerService.MSG_REGISTER_CLIENT);  
  4.      msg. replyTo = mMessenger ;  
  5.      //发送Message  
  6.      mService.send(msg);  
<4>Service后台接收Messenger对象并将该对象添加到集合中,至此Service后台便持有客户端前台的Messenger对象,双向通信建立起来了。
[java] view plaincopy
  1. switch (msg.what ) {  
  2.        case MSG_REGISTER_CLIENT :  
  3.        mClients.add(msg.replyTo );  
  4.        break;  
  5.        .....  
接着上面的继续讲,当客户端前台接收到Service后台发送到的IBinder对象并借此初始化Messenger对象后,借此对象将前台MainActivity对象的哈希值发送给Service后台:
[java] view plaincopy
  1. //将本对象的哈希值发送给Service后台  
  2.     msg = Message. obtain(null,  
  3.     MessengerService.MSG_SET_VALUE , this.hashCode(), 0);  
  4.     mService.send(msg);  
       Service后台接收到客户端前台发送的Message(MainActivity对象的哈希值)后再将该Message从后台返回发送给客户端前台,客户端前台接收到Message后将MainActivity对象的哈希值显示在主界面上:
[java] view plaincopy
  1. //创建一个继承Handler的子类IncomingHandler,用于接收Service后台发送过来的Message(本对象的哈希值)  
  2.      class IncomingHandler extends Handler {  
  3.          @Override  
  4.          public void handleMessage(Message msg) {  
  5.              switch (msg.what ) {  
  6.                  case MessengerService.MSG_SET_VALUE :  
  7.                      mCallbackText.setText("Received from service: " + msg.arg1);  
  8.                      break;  
  9.                  default:  
  10.                      super.handleMessage(msg);  
  11.              }  
  12.          }  
  13.      }  
      至此,我们便将该实例分析完成了,我们再来稍微总结下:实际上该实例原理就是分别在前台和后台建立并实例化各自的Handler和Messenger对象,并借此来实现前台和后台的双向通信,那么该实例实现的功能就是借助建立起来的双向通信,将前台得到的MainActivity对象的哈希值作为Message对象发送给后台,然后后台再将此哈希值作为Message对象发送到前台,前台最后将接收的Message展现在界面上。此实例模拟的是前台和后台,它们是属于同一进程当中,即使前台和后台不在同一进程中,也能实现双向通信,因为Messenger是借助IBinder来实现的,而Android远程调用(就是跨进程调用)就是通过IBinder实现的,可想而知,使用Messenger来实现进程间的通信是可行的。文不如表,表不如图,下面我们将用一张图来展示该实例运行的机制,如图:
      以上图示完整地展示了实例的执行过程,若分析程序有困难时,可以参考该图来理清思路。
至此,我们介绍了两种绑定Service的方法,还有一种方法可以执行进程间的通信,就是使用AIDL。由于篇幅关系,我们将会在下一篇博文中详细介绍AIDL的原理和使用方法。
      小结:当只要在本应用程序而无需跨进程绑定Service的时候,继承Binder的方法无疑是最好的方式,而若需要执行跨进程的工作时,就应该考虑使用Messenger了,Messenger其实是借助了Handler 和IBinder完成了消息的传输和远程的访问。

5、Bound Service的生命周期

      当所有的客户端都解除与Service的绑定后,Android系统会自动销毁Service。所以当只是纯粹地绑定Service,用户是不需要需管理Service的周期的,但是如果选择实现onStartCommand()回调方法,那么必须显式地停止service,因为service此时被看做是开启的。
      这种情况下,service会一直运行到它自己调用 stopSelf()或另一个组件调用stopService(),不论它是否和客户端绑定。
      另外,如果service被开启并且接受绑定,那么当系统调用 onUnbind()方法时,如果想要在下次客户端绑定的时候接受一个onRebind()的调用(而不是调用 onBind()),可以选择在 onUnbind()中返回true。
     onRebind()的返回值为void,但是客户端仍然在它的 onServiceConnected()回调方法中得到 IBinder 对象。
     下图展示了这种service(被开启,还允许绑定)的生命周期:



源代码下载,请戳这里!
0 0
原创粉丝点击