Android使用binder访问service的方式(一)

来源:互联网 发布:期货仿真交易软件 编辑:程序博客网 时间:2024/04/29 17:33

binder机制是贯穿整个android系统的进程间访问机制,经常被用来访问service,我们结合代码看一下binder在访问service的情形下是怎么具体使用的。


service 你可以理解成没有的界面的activity,它是跑在后台的程序,所谓后台是相对于可以被看得到的程序的,后台程序是不能直接交互的程序。

binder主要是用来进程间通信的,但也可用在和本地service通信。


1. 我们先来看一个与本地service通信的例子。

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package com.ckt.wangxin;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.Binder;  
  6. import android.os.IBinder;  
  7. import android.widget.Toast;  
  8. /** 
  9.  * This is a service stub for both LocalBinderClient 
  10.  * and RemoteBinderClient  
  11.  * @author Wang Xin 
  12.  * @email springnap@163.com 
  13.  * 
  14.  */  
  15. public class LocalService extends Service {  
  16.   
  17.     @Override  
  18.     public IBinder onBind(Intent intent) {  
  19.         return new LocalBinder();  
  20.     }     
  21.           
  22.     public void sayHelloWorld(){  
  23.         Toast.makeText(this.getApplicationContext(), "Hello World Local Service!", Toast.LENGTH_SHORT).show();  
  24.     }     
  25.           
  26.     public class LocalBinder extends Binder {  
  27.         LocalService getService() {  
  28.             // Return this instance of LocalService so clients can call public methods  
  29.             return LocalService.this;  
  30.         }     
  31.     }     
  32. }  

local servcie 的代码如上,在onBinder方法中返回binder,binder包含了service的句柄,客户端得到句柄以后就可以调用servcie的公共方法了,这种调用方式是最常见的。


客户端代码

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package com.ckt.wangxin;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11.   
  12. import com.ckt.wangxin.LocalService.LocalBinder;  
  13.   
  14. public class LocalServiceTestActivity extends Activity {  
  15.     static final String TAG = "LocalBinderTestActivity";  
  16.     ServiceConnection mSc;  
  17.       
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.           
  24.         mSc = new ServiceConnection(){  
  25.             @Override  
  26.             public void onServiceConnected(ComponentName name, IBinder service) {  
  27.                 Log.d(TAG, "service connected");  
  28.                 LocalService ss = ((LocalBinder)service).getService();  
  29.                 ss.sayHelloWorld();  
  30.             }  
  31.   
  32.             @Override  
  33.             public void onServiceDisconnected(ComponentName name) {  
  34.                 Log.d(TAG, "service disconnected");  
  35.             }  
  36.         };  
  37.     }  
  38.       
  39.     @Override  
  40.     protected void onStart() {  
  41.         super.onStart();  
  42.         Log.d(TAG, this.getApplicationContext().getPackageCodePath());  
  43.         Intent service = new Intent(this.getApplicationContext(),LocalService.class);  
  44.         this.bindService(service, mSc, Context.BIND_AUTO_CREATE);  
  45.     }  
  46.   
  47.     @Override  
  48.     protected void onStop() {  
  49.         super.onStop();  
  50.         //must unbind the service otherwise the ServiceConnection will be leaked.  
  51.         <span style="color: rgb(255, 0, 0); ">this.unbindService(mSc);</span>  
  52.     }  
  53. }  

需要注意的是在onStop中要解绑定service, 否则会造成内存泄露的问题。


2. 我们再看一下与另外一个进程中的service进行通信的问题(跨进程通信!)。

如何将servcie运行在另外一个进程呢?在manifest 里面配置个属性就行了。

android:process=":remote" , 代表这个service运行在同一个应用程序的不同进程中。


[html] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.ckt.wangxin"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="15" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".LocalServiceTestActivity"  
  14.             android:label="@string/app_name" >  
  15.            <!--  <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.                 <category android:name="android.intent.category.LAUNCHER" />  
  18.             </intent-filter> -->  
  19.         </activity>  
  20.         <service android:name=".LocalService"></service>  
  21.         <!-- android:process=":remote" specify this service run in   
  22.         another process in the same application. -->  
  23.         <service android:name=".RemoteService" android:process=":remote"></service>  
  24.         <activity android:name="RemoteServiceTestActivity">  
  25.             <intent-filter>  
  26.                 <action android:name="android.intent.action.MAIN" />  
  27.                 <category android:name="android.intent.category.LAUNCHER" />  
  28.             </intent-filter>  
  29.               
  30.         </activity>  
  31.     </application>  
  32.   
  33. </manifest>  


客户端可以使用Messenger发送消息到service。

客户端代码:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package com.ckt.wangxin;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.IBinder;  
  11. import android.os.Message;  
  12. import android.os.Messenger;  
  13. import android.os.RemoteException;  
  14. import android.util.Log;  
  15. import android.widget.Toast;  
  16.   
  17. public class RemoteServiceTestActivity extends Activity {  
  18.     static final String TAG = "RemoteServiceTestActivity";  
  19.     ServiceConnection mSc;  
  20.     public static final int SAY_HELLO_TO_CLIENT = 0;  
  21.     /** 
  22.      * Handler of incoming messages from service. 
  23.      */  
  24.     class IncomingHandler extends Handler {  
  25.         @Override  
  26.         public void handleMessage(Message msg) {  
  27.             switch (msg.what) {  
  28.                 case SAY_HELLO_TO_CLIENT:  
  29.                     Toast.makeText(RemoteServiceTestActivity.this.getApplicationContext(), "Hello World Remote Client!",  
  30.                             Toast.LENGTH_SHORT).show();  
  31.                     break;  
  32.                 default:  
  33.                     super.handleMessage(msg);  
  34.             }  
  35.         }  
  36.     }  
  37.       
  38.     Messenger messenger_reciever = new Messenger(new IncomingHandler());  
  39.       
  40.     /** Called when the activity is first created. */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.main);  
  45.           
  46.         mSc = new ServiceConnection(){  
  47.             @Override  
  48.             public void onServiceConnected(ComponentName name, IBinder service) {  
  49.                 Log.d(TAG, "service connected");  
  50.                 <span style="color: rgb(204, 0, 0); ">Messenger messenger = new Messenger(service);  
  51.                 Message msg = new Message();  
  52.                 msg.what = RemoteService.MSG_SAY_HELLO;</span>  
  53.                 msg.replyTo = messenger_reciever;  
  54.                 try {  
  55.                     <span style="color: rgb(255, 0, 0); ">messenger.send(msg);</span>  
  56.                 } catch (RemoteException e) {  
  57.                     e.printStackTrace();  
  58.                 }  
  59.             }  
  60.   
  61.             @Override  
  62.             public void onServiceDisconnected(ComponentName name) {  
  63.                 Log.d(TAG, "service disconnected");  
  64.             }  
  65.         };  
  66.     }  
  67.       
  68.     @Override  
  69.     protected void onStart() {  
  70.         super.onStart();  
  71.         Log.d(TAG, this.getApplicationContext().getPackageCodePath());  
  72.         Intent service = new Intent(this.getApplicationContext(),RemoteService.class);  
  73.         this.bindService(service, mSc, Context.BIND_AUTO_CREATE);  
  74.     }  
  75.   
  76.     @Override  
  77.     protected void onStop() {  
  78.         super.onStop();  
  79.         //must unbind the service otherwise the ServiceConnection will be leaked.  
  80.         this.unbindService(mSc);  
  81.     }  
  82. }  

获得service端传来的binder,用来构建一个Messenger向service发送消息。


service端代码:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package com.ckt.wangxin;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.Handler;  
  6. import android.os.IBinder;  
  7. import android.os.Message;  
  8. import android.os.Messenger;  
  9. import android.os.RemoteException;  
  10. import android.widget.Toast;  
  11.   
  12. public class RemoteService extends Service {  
  13.   
  14.     public static final int MSG_SAY_HELLO = 0;  
  15.   
  16.     @Override  
  17.     public IBinder onBind(Intent intent) {  
  18.       <span style="color: rgb(204, 0, 0); ">  return messager.getBinder();</span>  
  19.     }  
  20.   
  21.     Handler IncomingHandler = new Handler() {  
  22.   
  23.         @Override  
  24.         public void handleMessage(Message msg) {  
  25.             if(msg.replyTo != null){  
  26.                 Message msg_client = this.obtainMessage();  
  27.                 msg_client.what = RemoteServiceTestActivity.SAY_HELLO_TO_CLIENT;  
  28.                 try {  
  29.                     ((Messenger)msg.replyTo).send(msg_client);  
  30.                 } catch (RemoteException e) {  
  31.                     // TODO Auto-generated catch block  
  32.                     e.printStackTrace();  
  33.                 }  
  34.             }  
  35.             switch (msg.what) {  
  36.                 case MSG_SAY_HELLO:  
  37.                     Toast.makeText(RemoteService.this.getApplicationContext(), "Hello World Remote Service!",  
  38.                             Toast.LENGTH_SHORT).show();  
  39.                     break;  
  40.                 default:  
  41.             super.handleMessage(msg);  
  42.             }  
  43.         }  
  44.   
  45.     };  
  46.       
  47.     Messenger  messager = new Messenger (IncomingHandler);  
  48. }  

构建一个Messenger,包含一个handler,然后将messenger的binder传给客户端,客户端可以通过handler再构造一个messenger与service通信,消息在handler里面被处理。

现在是service端单向响应客户端的消息,同理可以做成双向发送消息,实现双向通信。


0 0