现在的位置: 首页 > 综合 > 正文

Android Service 之三(Bind Service, 继承自 Binder 类)

2018年03月20日 ⁄ 综合 ⁄ 共 5612字 ⁄ 字号 评论关闭

http://rainbow702.iteye.com/blog/1144521

之前提及过,启动Service有两种方式:startService 与 bindService。前者已经说过如何使用,所以,这篇贴子主要是关于 bind service的。 这里所讨论的是仅针对那些被绑定的service的,而那些既被startService() 又被 bindService() 的 service 不在此范围内。

① Bind Service就像是C/S架构中的服务端,其他组件(比如 Activity)绑定到它(通过 bindService()),可以向它发送请求,可以接受从它返回的响应,它甚至还提供了进程间通信(IPC)功能。

② 一个service要想能够被其他组件绑定,那么它的 onBind() 方法必须被实现,且必须返回一个 IBinder 对象,然后其他组件可以通过这个 IBinder 对象与该 service 进行通讯。

③ 多个client可以绑定至同一个service,但该 service 的onBind() 方法只会在第一个 client 绑定至其的时候被调用,当其他 client 再次绑定到它的时候,并不会调用  onBind() 方法,而是直接返回第一次被调用时产生的那个 IBinder 对象。也就是说,在其生命周期内,onBind() 只会被调用一次。

④ Bind Service 的生命周期如下图所示:



 

⑤ Bind Service 不会在后台无限期的一直运行,而是当所有绑定至其的组件都调用了 unbindService() 进行解绑之后,系统就会将其停掉以回收资源。

 

⑥ 当我们要实现一个 Bind Service 的时候,最重要的就是实现它的 onBind() 方法以返回一个 IBinder 对象

 

要生成一个 Bound Service ,共有三种方式:继承自 Binder 类,使用 Messenger ,使用 AIDL。下面且听小生一一道来。

第一种:继承自 Binder 类

需要注意的是,这种方式仅仅适用于这种场合:service 与 application 在同一个进程中。这种场合也是最最常见的。

它分以下几个步骤:

a. 在你的 service 类中声明一个内部类来继承 Binder 类。在该内部类中,最好提供一个公共方法来返回你的 service 实例。

b. 在你的 service 类中需要声明一个这个内部类的实例,以供在 onBind() 方法中返回

c. 在 client 端,在 onServiceConnected() 方法中得到从 onBind() 方法中返回的 IBinder 对象,然后可以通过该 对象中的公共方法得到相应的 service 实例,正如 第一个步骤 所说的那样。

d. 在 service 中提供公共方法, 这样就可以在组件(如 Activity 中调用这些公共方法了)

 

下面给出一例:

service 代码

Java代码  收藏代码
  1. public class BindServiceWithIBinder extends Service {  
  2.   
  3.     private static final String TAG = "BindServiceWithIBinder";  
  4.   
  5.     private final MyIBinder myIBinder = new MyIBinder();  
  6.   
  7.     /** 
  8.      * bindService() 时,调用的是这个方法,而非 onStartCommnad() 方法 
  9.      */  
  10.     @Override  
  11.     public IBinder onBind(Intent intent) {  
  12.         // 在主 Activity 上的 TextView 中打印出一行LOG  
  13.         MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  14.                 MyServiceActivity.UPDATE_VIEW, TAG + " ----> onBind"));  
  15.         return myIBinder;  
  16.     }  
  17.   
  18.     @Override  
  19.     public void onCreate() {  
  20.         MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  21.                 MyServiceActivity.UPDATE_VIEW, TAG + " ----> onCreate"));  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onDestroy() {  
  26.         MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  27.                 MyServiceActivity.UPDATE_VIEW, TAG + " ----> onDestroy"));  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onRebind(Intent intent) {  
  32.         MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  33.                 MyServiceActivity.UPDATE_VIEW, TAG + " ----> onRebind"));  
  34.     }  
  35.   
  36.     @Override  
  37.     public int onStartCommand(Intent intent, int flags, int startId) {  
  38.         MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  39.                 MyServiceActivity.UPDATE_VIEW, TAG + " ----> onStartCommand"));  
  40.         return START_STICKY;  
  41.     }  
  42.   
  43.     @Override  
  44.     public boolean onUnbind(Intent intent) {  
  45.         MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  46.                 MyServiceActivity.UPDATE_VIEW, TAG + " ----> onUnbind"));  
  47.         return super.onUnbind(intent);  
  48.     }  
  49.   
  50.     /** 
  51.      * 声明一个 Binder 类的实现类,供在 onBind() 方法中返回该类的一个实例 
  52.      * @author 001718 
  53.      * 
  54.      */  
  55.     public class MyIBinder extends Binder {  
  56.         public Service getService() {  
  57.             MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  58.                     MyServiceActivity.UPDATE_VIEW,  
  59.                     "BindServiceWithIBinder.MyIBinder.getService()"));  
  60.             return BindServiceWithIBinder.this;  
  61.         }  
  62.     }  
  63.   
  64.     /** 
  65.      * service 提供的公共方法,在activity中可以调用 
  66.      */  
  67.     public void download() {  
  68.         try {  
  69.             MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  70.                     MyServiceActivity.UPDATE_VIEW, TAG  
  71.                             + " ----> download(): 文件下载中..."));  
  72.             Thread.sleep(3000);  
  73.             MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  74.                     MyServiceActivity.UPDATE_VIEW, TAG  
  75.                             + " ----> download(): 文件下载完成..."));  
  76.         } catch (InterruptedException e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.     }  
  80. }  

 主 Activity 中的相应关键代码为:

Java代码  收藏代码
  1. private void doUnbindService() {  
  2.         if (isBound) {  
  3.             unbindService(myLocalServiceConnection);  
  4.             isBound = false;  
  5.         }  
  6.     }  
  7.   
  8.     private void doBindService() {  
  9.         Log.i("bind""begin to bind");  
  10.         bindService(intent, myLocalServiceConnection, Context.BIND_AUTO_CREATE);  
  11.   
  12.     }  
  13.   
  14.     private ServiceConnection myLocalServiceConnection = new ServiceConnection() {  
  15.         public void onServiceConnected(android.content.ComponentName name,  
  16.                 android.os.IBinder service) {  
  17.             MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  18.                     MyServiceActivity.UPDATE_VIEW,  
  19.                     "myServiceConnection.onServiceConnected"));  
  20.             // 因为 客户端 与 服务 在同一个进程内,这样一来,就可以知道参数 "service"的类型了,也就可以进行显示的强制类型转换了。  
  21.             // 而如果 客户端与服务不在同一个进程中的话,那么此处是不可以进行显示强制类型转换的,  
  22.             // 因为,通过Debug,可以发现此时传进来的 Service 的类型是 BinderProxy  
  23.             MyIBinder myIBinder = (MyIBinder) service;  
  24.             bsi = (BindServiceWithIBinder) myIBinder.getService();  
  25.             isBound = true;  
  26.   
  27.             bsi.download();  
  28.         };  
  29.   
  30.         public void onServiceDisconnected(android.content.ComponentName name) {  
  31.             MyServiceActivity.vh.sendMessage(MyServiceActivity.createMessage(  
  32.                     MyServiceActivity.UPDATE_VIEW,  
  33.                     "myServiceConnection.onServiceDisconnected"));  
  34.             isBound = false;  
  35.         };  
  36.     };  

 

 下面来看运行效果:

连续点击两次 Bind Service



  

从此图中可以看出,bind service 的响应过程。也可以看到,第二次点击时,service 没作任何反应,因为当前 Activity 在第一次点击后就已经跟此service绑定了。

点击 Unbind Service



 至此,该 service 的生命周期结束,它也会被系统给停掉以回收资源。

抱歉!评论已关闭.