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

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

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

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

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

  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. }  
package com.ckt.wangxin;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
/**
 * This is a service stub for both LocalBinderClient
 * and RemoteBinderClient 
 * @author Wang Xin
 * @email springnap@163.com
 *
 */
public class LocalService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return new LocalBinder();
    }   
        
    public void sayHelloWorld(){
        Toast.makeText(this.getApplicationContext(), "Hello World Local Service!", Toast.LENGTH_SHORT).show();
    }   
        
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }   
    }   
}

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

客户端代码

  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. }  
package com.ckt.wangxin;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.ckt.wangxin.LocalService.LocalBinder;

public class LocalServiceTestActivity extends Activity {
    static final String TAG = "LocalBinderTestActivity";
    ServiceConnection mSc;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mSc = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "service connected");
                LocalService ss = ((LocalBinder)service).getService();
                ss.sayHelloWorld();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "service disconnected");
            }
        };
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, this.getApplicationContext().getPackageCodePath());
        Intent service = new Intent(this.getApplicationContext(),LocalService.class);
        this.bindService(service, mSc, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //must unbind the service otherwise the ServiceConnection will be leaked.
        <span style="color: rgb(255, 0, 0); ">this.unbindService(mSc);</span>
    }
}

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

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

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

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

  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>  
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ckt.wangxin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LocalServiceTestActivity"
            android:label="@string/app_name" >
           <!--  <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter> -->
        </activity>
        <service android:name=".LocalService"></service>
        <!-- android:process=":remote" specify this service run in 
        another process in the same application. -->
        <service android:name=".RemoteService" android:process=":remote"></service>
        <activity android:name="RemoteServiceTestActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
        </activity>
    </application>

</manifest>

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

客户端代码:

  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. }  
package com.ckt.wangxin;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class RemoteServiceTestActivity extends Activity {
    static final String TAG = "RemoteServiceTestActivity";
    ServiceConnection mSc;
    public static final int SAY_HELLO_TO_CLIENT = 0;
    /**
     * Handler of incoming messages from service.
     */
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SAY_HELLO_TO_CLIENT:
                	Toast.makeText(RemoteServiceTestActivity.this.getApplicationContext(), "Hello World Remote Client!",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }
    
    Messenger messenger_reciever = new Messenger(new IncomingHandler());
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mSc = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "service connected");
                <span style="color: rgb(204, 0, 0); ">Messenger messenger = new Messenger(service);
                Message msg = new Message();
                msg.what = RemoteService.MSG_SAY_HELLO;</span>
                msg.replyTo = messenger_reciever;
                try {
					<span style="color: rgb(255, 0, 0); ">messenger.send(msg);</span>
				} catch (RemoteException e) {
					e.printStackTrace();
				}
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "service disconnected");
            }
        };
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, this.getApplicationContext().getPackageCodePath());
        Intent service = new Intent(this.getApplicationContext(),RemoteService.class);
        this.bindService(service, mSc, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //must unbind the service otherwise the ServiceConnection will be leaked.
        this.unbindService(mSc);
    }
}

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

service端代码:

  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. }  
package com.ckt.wangxin;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.widget.Toast;

public class RemoteService extends Service {

    public static final int MSG_SAY_HELLO = 0;

    @Override
    public IBinder onBind(Intent intent) {
      <span style="color: rgb(204, 0, 0); ">  return messager.getBinder();</span>
    }

    Handler IncomingHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
        	if(msg.replyTo != null){
        		Message msg_client = this.obtainMessage();
        		msg_client.what = RemoteServiceTestActivity.SAY_HELLO_TO_CLIENT;
        		try {
					((Messenger)msg.replyTo).send(msg_client);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
            switch (msg.what) {
                case MSG_SAY_HELLO:
                    Toast.makeText(RemoteService.this.getApplicationContext(), "Hello World Remote Service!",
                            Toast.LENGTH_SHORT).show();
                    break;
                default:
            super.handleMessage(msg);
            }
        }

    };
    
    Messenger  messager = new Messenger (IncomingHandler);
}

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

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

原文地址:https://www.cnblogs.com/lzjsky/p/4938256.html