Android技术22:Android中AIDL
在Android中进程内部通过全局变量,文件,preference,数据库作为数据的载体实现数据共享和通信。然后在进程之间则需要借助Binder实现IPC调用。Android进程通信框架:服务端,客户端,Linux binder驱动。Binder驱动成为连接两端的桥梁。我们首先通过aidl语言实现一个简单的多进程通信。具体实现步骤如下:
1.定义aidl文件
IService.aidl,定义一个接口,test() ,不包含负责的类和数据。
1 package com.demo.ipc; 2 3 interface IService{ 4 void test(); 5 }
2.在gen目录下自动生成响应java文件,其主要类下生成一个Stub,该类为抽象类,继承了IBinder类,主要作为服务端,用于接收服务请求,与客户端打交道。
1 /* 2 * This file is auto-generated. DO NOT MODIFY. 3 * Original file: D:\\Android\\ipc\\src\\com\\demo\\ipc\\IService.aidl 4 */ 5 package com.demo.ipc; 6 public interface IService extends android.os.IInterface 7 { 8 /** Local-side IPC implementation stub class. */ 9 public static abstract class Stub extends android.os.Binder implements com.demo.ipc.IService 10 { 11 private static final java.lang.String DESCRIPTOR = "com.demo.ipc.IService"; 12 /** Construct the stub at attach it to the interface. */ 13 public Stub() 14 { 15 this.attachInterface(this, DESCRIPTOR); 16 } 17 /** 18 * Cast an IBinder object into an com.demo.ipc.IService interface, 19 * generating a proxy if needed. 20 */ 21 public static com.demo.ipc.IService asInterface(android.os.IBinder obj) 22 { 23 if ((obj==null)) { 24 return null; 25 } 26 android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); 27 if (((iin!=null)&&(iin instanceof com.demo.ipc.IService))) { 28 return ((com.demo.ipc.IService)iin); 29 } 30 return new com.demo.ipc.IService.Stub.Proxy(obj); 31 } 32 @Override public android.os.IBinder asBinder() 33 { 34 return this; 35 } 36 @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException 37 { 38 switch (code) 39 { 40 case INTERFACE_TRANSACTION: 41 { 42 reply.writeString(DESCRIPTOR); 43 return true; 44 } 45 case TRANSACTION_test: 46 { 47 data.enforceInterface(DESCRIPTOR); 48 this.test(); 49 reply.writeNoException(); 50 return true; 51 } 52 } 53 return super.onTransact(code, data, reply, flags); 54 } 55 private static class Proxy implements com.demo.ipc.IService 56 { 57 private android.os.IBinder mRemote; 58 Proxy(android.os.IBinder remote) 59 { 60 mRemote = remote; 61 } 62 @Override public android.os.IBinder asBinder() 63 { 64 return mRemote; 65 } 66 public java.lang.String getInterfaceDescriptor() 67 { 68 return DESCRIPTOR; 69 } 70 @Override public void test() throws android.os.RemoteException 71 { 72 android.os.Parcel _data = android.os.Parcel.obtain(); 73 android.os.Parcel _reply = android.os.Parcel.obtain(); 74 try { 75 _data.writeInterfaceToken(DESCRIPTOR); 76 mRemote.transact(Stub.TRANSACTION_test, _data, _reply, 0); 77 _reply.readException(); 78 } 79 finally { 80 _reply.recycle(); 81 _data.recycle(); 82 } 83 } 84 } 85 static final int TRANSACTION_test = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); 86 } 87 public void test() throws android.os.RemoteException; 88 }
3.创建服务端RemoteService
重写onBind()方法。
1 public class RemoteService extends Service { 2 3 @Override 4 public IBinder onBind(Intent intent) { 5 return mBinder; 6 } 7 8 IService.Stub mBinder=new Stub() { 9 10 @Override 11 public void test() throws RemoteException { 12 Log.i("tag", "RemoteService--mBinder--add"); 13 14 } 15 }; 16 17 }
5.AndroidManifest.xml注册声明Service
1 <service android:name="com.demo.ipc.RemoteService" android:process=":remote"> 2 <intent-filter> 3 <action android:name="com.demo.ipc.RemoteService"/> 4 </intent-filter> 5 </service>
6.定义客户端
1 package com.demo.ipc; 2 3 import android.content.ComponentName; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.os.RemoteException; 10 import android.support.v7.app.ActionBarActivity; 11 import android.util.Log; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.Button; 15 16 public class MainActivity extends ActionBarActivity { 17 18 private Button bt1,bt2; 19 private IService mService; 20 private boolean isOpen=false; 21 private ServiceConnection serviceConnection=new ServiceConnection() { 22 23 @Override 24 public void onServiceDisconnected(ComponentName name) { 25 Log.i("tag", "MainActivity--ServiceConnextion--onServiceDisconnected"); 26 mService=null; 27 } 28 29 @Override 30 public void onServiceConnected(ComponentName name, IBinder service) { 31 Log.i("tag", "MainActivity--ServiceConnextion--onServiceConnected"); 32 mService=IService.Stub.asInterface(service); 33 } 34 }; 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.activity_main); 39 bt1=(Button) findViewById(R.id.bt1); 40 bt2=(Button) findViewById(R.id.bt2); 41 bt1.setOnClickListener(new OnClickListener() { 42 43 @Override 44 public void onClick(View v) { 45 if(!isOpen){ 46 bindService(new Intent("com.demo.ipc.RemoteService"), serviceConnection, Context.BIND_AUTO_CREATE); 47 }else{ 48 unbindService(serviceConnection); 49 } 50 51 } 52 }); 53 bt2.setOnClickListener(new OnClickListener() { 54 55 @Override 56 public void onClick(View v) { 57 try { 58 mService.test(); 59 } catch (RemoteException e) { 60 e.printStackTrace(); 61 } 62 63 } 64 }); 65 } 66 67 68 }
7.显示结果
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。