Android IPC 进程间通信 aidl

AIDL的使用

实例:

有一个打印服务需要进行进程间通信,

1.在本地写一个aidl文件接口

2.启动远程service服务,bindService中在ServiceConnection得到打印服务,

3.调用远程服务aidl方法


如果对Android比较熟悉,那么一定使用过AIDL,如果你还不了解,那么也没关系,下面会使用一个例子展示AIDL的用法。

我们使用AIDL实现一个跨进程的加减法调用

1、服务端

新建一个项目,创建一个包名:com.zhy.calc.aidl,在包内创建一个ICalcAIDL文件:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.zhy.calc.aidl;  
  2. interface ICalcAIDL  
  3. {  
  4.     int add(int x , int y);  
  5.     int min(int x , int y );  
  6. }  

注意,文件名为ICalcAIDL.aidl

然后在项目的gen目录下会生成一个ICalcAIDL.java文件,暂时不贴这个文件的代码了,后面会详细说明

然后我们在项目中新建一个Service,代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.zhy_binder;  
  2.   
  3. import com.zhy.calc.aidl.ICalcAIDL;  
  4.   
  5. import android.app.Service;  
  6. import android.content.Intent;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9. import android.util.Log;  
  10.   
  11. public class CalcService extends Service  
  12. {  
  13.     private static final String TAG = "server";  
  14.  
  15.     public IBinder onBind(Intent t)  
  16.     {  
  17.         Log.e(TAG, "onBind");  
  18.         return mBinder;  
  19.     }  
  20.   
  21.     private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub()  
  22.     {  
  23.   
  24.         @Override  
  25.         public int add(int x, int y) throws RemoteException  
  26.         {  
  27.             return x + y;  
  28.         }  
  29.   
  30.         @Override  
  31.         public int min(int x, int y) throws RemoteException  
  32.         {  
  33.             return x - y;  
  34.         }  
  35.   
  36.     };  
  37.   
  38. }  
在此Service中,使用生成的ICalcAIDL创建了一个mBinder的对象,并在Service的onBind方法中返回

最后记得在AndroidManifest中注册

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <service android:name="com.example.zhy_binder.CalcService" >  
  2.            <intent-filter>  
  3.                <action android:name="com.zhy.aidl.calc" />  
  4.   
  5.                <category android:name="android.intent.category.DEFAULT" />  
  6.            </intent-filter>  
  7.        </service>  

这里我们指定了一个name,因为我们一会会在别的应用程序中通过Intent来查找此Service;这个不需要Activity,所以我也就没写Activity,安装完成也看不到安装图标,悄悄在后台运行着。

到此,服务端编写完毕。下面开始编写客户端

2、客户端

客户端的代码比较简单,创建一个布局,里面包含4个按钮,分别为绑定服务,解除绑定,调用加法,调用减法

布局文件:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:onClick="bindService"  
  11.         android:text="BindService" />  
  12.   
  13.     <Button  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:onClick="unbindService"  
  17.         android:text="UnbindService" />  
  18.   
  19.     <Button  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:onClick="addInvoked"  
  23.         android:text="12+12" />  
  24.   
  25.     <Button  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:onClick="minInvoked"  
  29.         android:text="50-12" />  
  30.   
  31. </LinearLayout>  

主Activity

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.zhy_binder_client;  
  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. import android.view.View;  
  12. import android.widget.Toast;  
  13.   
  14. import com.zhy.calc.aidl.ICalcAIDL;  
  15.   
  16. public class MainActivity extends Activity  
  17. {  
  18.     private ICalcAIDL mCalcAidl;  
  19.   
  20.     private ServiceConnection mServiceConn = new ServiceConnection()  
  21.     {  
  22.         @Override  
  23.         public void onServiceDisconnected(ComponentName name)  
  24.         {  
  25.             Log.e("client""onServiceDisconnected");  
  26.             mCalcAidl = null;  
  27.         }  
  28.   
  29.         @Override  
  30.         public void onServiceConnected(ComponentName name, IBinder service)  
  31.         {  
  32.             Log.e("client""onServiceConnected");  
  33.             mCalcAidl = ICalcAIDL.Stub.asInterface(service);  
  34.         }  
  35.     };  
  36.   
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState)  
  39.     {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.activity_main);  
  42.   
  43.     }  
  44.       
  45.     /** 
  46.      * 点击BindService按钮时调用 
  47.      * @param view 
  48.      */  
  49.     public void bindService(View view)  
  50.     {  
  51.         Intent intent = new Intent();  
  52.         intent.setAction("com.zhy.aidl.calc");  
  53.         bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);  
  54.     }  
  55.     /** 
  56.      * 点击unBindService按钮时调用 
  57.      * @param view 
  58.      */  
  59.     public void unbindService(View view)  
  60.     {  
  61.         unbindService(mServiceConn);  
  62.     }  
  63.     /** 
  64.      * 点击12+12按钮时调用 
  65.      * @param view 
  66.      */  
  67.     public void addInvoked(View view) throws Exception  
  68.     {  
  69.   
  70.         if (mCalcAidl != null)  
  71.         {  
  72.             int addRes = mCalcAidl.add(1212);  
  73.             Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show();  
  74.         } else  
  75.         {  
  76.             Toast.makeText(this"服务器被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT)  
  77.                     .show();  
  78.   
  79.         }  
  80.   
  81.     }  
  82.     /** 
  83.      * 点击50-12按钮时调用 
  84.      * @param view 
  85.      */  
  86.     public void minInvoked(View view) throws Exception  
  87.     {  
  88.   
  89.         if (mCalcAidl != null)  
  90.         {  
  91.             int addRes = mCalcAidl.min(5812);  
  92.             Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show();  
  93.         } else  
  94.         {  
  95.             Toast.makeText(this"服务端未绑定或被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT)  
  96.                     .show();  
  97.   
  98.         }  
  99.   
  100.     }  
  101.   
  102. }  

很标准的绑定服务的代码。

直接看运行结果:

我们首先点击BindService按钮,查看log

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 08-09 22:56:38.959: E/server(29692): onCreate  
  2. 08-09 22:56:38.959: E/server(29692): onBind  
  3. 08-09 22:56:38.959: E/client(29477): onServiceConnected  
可以看到,点击BindService之后,服务端执行了onCreate和onBind的方法,并且客户端执行了onServiceConnected方法,标明服务器与客户端已经联通

然后点击12+12,50-12可以成功的调用服务端的代码并返回正确的结果

下面我们再点击unBindService

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. 08-09 22:59:25.567: E/server(29692): onUnbind  
  2. 08-09 22:59:25.567: E/server(29692): onDestroy  
由于我们当前只有一个客户端绑定了此Service,所以Service调用了onUnbind和onDestory

然后我们继续点击12+12,50-12,通过上图可以看到,依然可以正确执行,也就是说即使onUnbind被调用,连接也是不会断开的,那么什么时候会端口呢?

即当服务端被异常终止的时候,比如我们现在在手机的正在执行的程序中找到该服务:


似乎和Binder框架没什么关系,下面我们来具体看一看AIDL为什么做了些什么。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值