Aidl实现跨进程通信小例子

来源:互联网 发布:黄乙玲心事谁人知 编辑:程序博客网 时间:2024/05/18 03:58

转:http://blog.csdn.net/u011001142/article/details/50927522


首先,先看一下Services端目录结构:


先在src目录下创建一个文件,命名为Count.aidl 此时在gen目录下就会自动创建一个Count.Java文件,把这个文件复制到Client项目的src文件下即可。

Service短的代码如下:

[java] view plain copy
 print?
  1. public class MyServer extends Service {  
  2.   
  3.   class Mylocation extends Count.Stub{  
  4.         @Override  
  5.         public double count(double a, double b) throws RemoteException {  
  6.             return a+b;  
  7.         }  
  8.     }  
  9.     @Override  
  10.     public IBinder onBind(Intent intent) {  
  11.         return new Mylocation();  
  12.     }  
  13.     @Override  
  14.     public boolean onUnbind(Intent intent) {  
  15.         return super.onUnbind(intent);  
  16.     }  
  17. }  
此时要在AndroidManifest.xml中注册:

 <service Android:name="com.zhongyan.server.MyServer" android:process=":remote" android:exported="true" >
            <intent-filter>
                <action android:name="com.zhongyan.server.bindServer"/>
            </intent-filter>
  </service>

切记跨进程通信一定要加这个属性 android:exported="true" ,该属性用来标示,其它应用的组件是否可以唤醒service或者和这个service进行交互:true可以,false不可以。如果为false,只有同一个应用的组件或者有着同样user ID的应用可以启动这个service或者绑定这个service。

Client端代码如下:

[java] view plain copy
 print?
  1. public class MainActivity extends Activity {  
  2.     private Button btnCalculate;  
  3.     private EditText etNum1;  
  4.     private EditText etNum2;  
  5.     private TextView tvResult;  
  6.     private Count mcount;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         etNum1 = (EditText) findViewById(R.id.et_num_one);  
  12.         etNum2 = (EditText) findViewById(R.id.et_num_two);  
  13.   
  14.         tvResult = (TextView) findViewById(R.id.tv_result);  
  15.         btnCalculate = (Button) findViewById(R.id.btn_cal);  
  16.   
  17.         Intent intent = new Intent();  
  18.         intent.setAction("com.zhongyan.server.bindServer");  
  19.         intent.setPackage("com.zhongyan.serveraidl");  
  20.         bindService(intent,connection,Context.BIND_AUTO_CREATE);  
  21.         btnCalculate.setOnClickListener(new OnClickListener() {  
  22.   
  23.             @Override  
  24.             public void onClick(View v) {  
  25.   
  26.                 try {  
  27.                     double num1 = Double.parseDouble(etNum1.getText().toString());  
  28.                     double num2 = Double.parseDouble(etNum2.getText().toString());  
  29.                     String result;  
  30.                     if (mcount!= null){  
  31.                         result = "计算结果:"+ mcount.count(num1, num2);  
  32.                         tvResult.setText(result);  
  33.                     }  
  34.   
  35.                 } catch (RemoteException e) {  
  36.                     e.printStackTrace();  
  37.                 }  
  38.             }  
  39.         });  
  40.     }  
  41.     private ServiceConnection connection = new ServiceConnection() {  
  42.   
  43.         @Override  
  44.         public void onServiceDisconnected(ComponentName name) {  
  45.             mcount = null;  
  46.         }  
  47.   
  48.         @Override  
  49.         public void onServiceConnected(ComponentName name, IBinder service) {  
  50.             mcount = Count.Stub.asInterface(service);  
  51.         }  
  52.     };  
  53. }  
Client的界面显示如下:


如果连接成功,输入数字,点击计算就可以看到结果了。


0 0
原创粉丝点击