/******************************************************************************** * Android AIDL自動生成Java文件測試 * 說明: * 知道有aidl這東西已經挺久了,但是一直沒有花時間來系統了解一下其工作機制,現在 * 花點時間一點一點驗證一下其功能。 * * 2016-5-8 深圳 南山平山村 曾劍鋒 *******************************************************************************/ 一、參考文章: 1. Android AIDL使用詳解 http://blog.csdn.net/stonecao/article/details/6425019 2. android進程間通信:使用AIDL http://blog.csdn.net/saintswordsman/article/details/5130947 二、cat forService.aidl package com.example.aidltest.aidl; import com.example.aidltest.aidl.forActivity; interface forService { void registerTestCall(forActivity cb); void invokCallBack(); } 三、cat forService.java /* * This file is auto-generated. DO NOT MODIFY. * Original file: D:\\soft\\Android\\workspace\\AidlTest\\src\\com\\example\\aidltest\\aidl\\forService.aidl */ package com.example.aidltest.aidl; // 繼承了android.os.IInterface接口 public interface forService extends android.os.IInterface { /** Local-side IPC implementation stub class. */ // Stub類繼承了Binder,並繼承我們在aidl文件中定義的接口 public static abstract class Stub extends android.os.Binder implements com.example.aidltest.aidl.forService { private static final java.lang.String DESCRIPTOR = "com.example.aidltest.aidl.forService"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.example.aidltest.aidl.forService * interface, generating a proxy if needed. */ // 在使用的時候經常會看到這一部分的內容 public static com.example.aidltest.aidl.forService asInterface( android.os.IBinder obj) { if ((obj == null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin != null) && (iin instanceof com.example.aidltest.aidl.forService))) { return ((com.example.aidltest.aidl.forService) iin); } return new com.example.aidltest.aidl.forService.Stub.Proxy(obj); } @Override public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_registerTestCall: { data.enforceInterface(DESCRIPTOR); com.example.aidltest.aidl.forActivity _arg0; _arg0 = com.example.aidltest.aidl.forActivity.Stub .asInterface(data.readStrongBinder()); this.registerTestCall(_arg0); reply.writeNoException(); return true; } case TRANSACTION_invokCallBack: { data.enforceInterface(DESCRIPTOR); this.invokCallBack(); reply.writeNoException(); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.example.aidltest.aidl.forService { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } @Override public void registerTestCall( com.example.aidltest.aidl.forActivity cb) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeStrongBinder((((cb != null)) ? (cb.asBinder()) : (null))); mRemote.transact(Stub.TRANSACTION_registerTestCall, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } @Override public void invokCallBack() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_invokCallBack, _data, _reply, 0); _reply.readException(); } finally { _reply.recycle(); _data.recycle(); } } } static final int TRANSACTION_registerTestCall = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); static final int TRANSACTION_invokCallBack = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1); } // AIDL中自定義的方法,沒有實現 public void registerTestCall(com.example.aidltest.aidl.forActivity cb) throws android.os.RemoteException; public void invokCallBack() throws android.os.RemoteException; } 四、cat MainActivity.java public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { ...... new forService.Stub() { @Override public void registerTestCall(forActivity cb) throws RemoteException { // TODO Auto-generated method stub } @Override public void invokCallBack() throws RemoteException { // TODO Auto-generated method stub } }; ...... } }