标签:
/******************************************************************************** * 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 } }; ...... } }
标签:
原文地址:http://www.cnblogs.com/zengjfgit/p/5469705.html