AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成能够在Android设备上两个进程之间进行进程间通讯(interprocess communication, IPC)的代码。若是在一个进程中(例如Activity)要调用另外一个进程中(例如Service)对象的操做,就可使用AIDL生成可序列化的参数。 java
AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值能够是任何类型,甚至是其余AIDL生成的接口。 android
AIDL只支持接口方法,不能公开static变量。 编程
其中AIDL的方法还提供了oneway这个关键字,能够用关键字oneway来标明远程调用的行为属性,使用了该关键字,那么远程调用将仅仅是调用所需的数据传输过来并当即返回,而不会等待结果的返回,也便是说不会阻塞远程线程的运行。AIDL接口将最终将得到一个从Binder线程池中产生的调用(和普通的远程调用相似)。若是关键字oneway在本地调用中被使用,将不会对函数调用有任何影响。 app
package com.liusl.aidl; import com.liusl.aidl.AIDLCallback; interface ServiceAIDL { void registerClient(AIDLCallback cb); void unregisterClient(AIDLCallback cb); int add(int i, int j); int sum(in int[] numbers); }
package com.liusl.aidl; interface AIDLCallback { int returnResult(int result); }
将AIDLCallback做为服务端给客户端的回调,这样才实现了服务端和客户端的通讯。同时使服务端继承service类,实现onBind方法,建立AIDLService对象,这样才能将服务端的接口提供给客户端进行调用。 编程语言
package com.liusl.aidl; import com.liusl.aidl.AIDLCallback; import com.liusl.aidl.ServiceAIDL; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MyAIDLService extends Service { public final String TAG = "[service]"; int sum = 0; @Override public IBinder onBind(Intent intent) { Log.i(TAG, "[MyAIDLService] onBind(intent)..."); return (IBinder) new ServiceAIDLImpl(); } class ServiceAIDLImpl extends ServiceAIDL.Stub { private AIDLCallback cb; @Override public void registerClient(AIDLCallback cb) throws RemoteException { this.cb = cb; Log.i(TAG, "[ServiceAIDLImpl] registerClient start..."); cb.asBinder().linkToDeath(new DeathRecipient() { @Override public void binderDied() { try { Log.i(TAG, "[ServiceAIDLImpl]binderDied."); } catch (Throwable e) { } } }, 0); Log.i(TAG, "[ServiceAIDLImpl] registerClient end..."); } @Override public void unregisterClient(AIDLCallback cb) throws RemoteException { } @Override public int add(int i, int j) throws RemoteException { sum = 0; sum = i + j; cb.returnResult(sum); return 0; } @Override public int sum(int[] numbers) throws RemoteException { sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } cb.returnResult(sum); return 0; } } }
在客户端使用服务端的AIDL前,须要先经过建立一个客户端与服务端的链接,步骤以下: ide
String actionName = "com.liusl.aidl.MyAIDLService"; 函数
Intent intent = new Intent(actionName) this
MyServiceConnection connection = new MyServiceConnection(); spa
经过调用bindService(intent, connection, Context.BIND_AUTO_CREATE);->onServiceConnected(),在OnServiceConnected()中获取服务端得aidl对象, .net
myservice = (ServiceAIDL) ServiceAIDL.Stub.asInterface(service);
private class AIDLCallbackImpl extends AIDLCallback.Stub { public int returnResult(int result) { textView.setText("Result :" + result); return result; } }
myservice.registerClient(callBack);
myservice.add(2, 3);
由此就完成了客户端注册一个客户到服务端而且调用服务端的add方法,服务端将add的结果反馈给回调方法,客户端在接收到回调传来的值后进行显示操做。
注意:须要在AndroidMainFest.xml中对服务进行注册:
<service android:name="com.liusl.aidl.MyAIDLService"> <intent-filter> <action android:name="com.liusl.aidl.MyAIDLService"/> </intent-filter> </service>
代码所在位置: