标签:
AIDL和其他的IDL类似,它允许你定义程序接口,以便客户端与服务器端通过IPC机制交互。在android上面,一个进程一般不能访问另外进程的内存。因此,Android平台将这些跨进程访问的对象分解成操作系统能够识别的简单对象。并为跨应用访问而特殊编排和整理这些对象。用于编排和整理这些对象的代码编写起来十分冗长,所以Android的AIDL提供了相关工具来自动生成这些代码。今天,我们开始AIDL的学习。
一,AIDL的定义:
它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
二、AIDL的开发流程:
具体的aidl的说明: http://www.cnblogs.com/popapa/p/android_aidl.html
为了测试,我写了aidl服务器端和aidl客户端!
项目结构如下:
一、 创建.aidl文件:IRemoteAIDL.aidl
// IRemoteAIDL.aidl package com.huhx.linux; // Declare any non-default types here with import statements interface IRemoteAIDL { int add(int a, int b); }
二、 定义服务类:RemoteService.java
package com.huhx.linux.aidlserver; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import com.huhx.linux.IRemoteAIDL; public class RemoteService extends Service { private final static String TAG = "RemoteService"; private IRemoteAIDL.Stub binder = new IRemoteAIDL.Stub() { @Override public int add(int a, int b) throws RemoteException { Log.i(TAG, "a = " + a + ", b = " + b); return a + b; } }; @Override public IBinder onBind(Intent intent) { return binder; } }
三、 在AndroidManifest.xml文件中声明服务:
<service android:name=".RemoteService" android:enabled="true" android:exported="true" />
项目结构如下:
一、 创建与服务器端一样的aidl文件: IRemoteAIDL.aidl
// IRemoteAIDL.aidl package com.huhx.linux; // Declare any non-default types here with import statements interface IRemoteAIDL { int add(int a, int b); }
二、 在MainActivity中,得到远程服务并调用它的方法。
package com.huhx.linux.aidlclient; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.huhx.linux.IRemoteAIDL; public class MainActivity extends AppCompatActivity { private IRemoteAIDL iRemoteAIDL; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); binServices(); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iRemoteAIDL = IRemoteAIDL.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { iRemoteAIDL = null; } }; // 绑定服务 private void binServices() { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.huhx.linux.aidlserver", "com.huhx.linux.aidlserver.RemoteService")); bindService(intent, conn, Context.BIND_AUTO_CREATE); } // 处理相加的方法 public void addMethod(View view) { try { int result = iRemoteAIDL.add(3, 4); Toast.makeText(MainActivity.this, "3 + 4 = " + result, Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } }
三、 根据aidl文件,android生成的接口如下(可以不关注):
/* * This file is auto-generated. DO NOT MODIFY. * Original file: J:\\android\\andridtest\\andridtest\\2016-02-29\\BaseTest8\\aidlclient\\src\\main\\aidl\\com\\huhx\\linux\\IRemoteAIDL.aidl */ package com.huhx.linux; // Declare any non-default types here with import statements public interface IRemoteAIDL extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements com.huhx.linux.IRemoteAIDL { private static final java.lang.String DESCRIPTOR = "com.huhx.linux.IRemoteAIDL"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an com.huhx.linux.IRemoteAIDL interface, * generating a proxy if needed. */ public static com.huhx.linux.IRemoteAIDL asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.huhx.linux.IRemoteAIDL))) { return ((com.huhx.linux.IRemoteAIDL)iin); } return new com.huhx.linux.IRemoteAIDL.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_add: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = this.add(_arg0, _arg1); reply.writeNoException(); reply.writeInt(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements com.huhx.linux.IRemoteAIDL { 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 int add(int a, int b) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); int _result; try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeInt(a); _data.writeInt(b); mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); } public int add(int a, int b) throws android.os.RemoteException; }
如果要跨进程传递某个类,可以通过 IPC 接口来实现。 不过,请务必确保在 IPC 通道的对端可以识别该类的代码,该类必须支持 Parcelable
接口。支持 Parcelable
接口非常重要,因为这使得 Android 系统可将对象分解为能够跨进程组装的原生数据。
可按以下步骤创建支持 Parcelable
协议的类:
Parcelable
接口。writeToParcel
方法,参数为当前对象的状态,并写入一个 Parcel
中。CREATOR
的静态成员变量,即为一个实现了 Parcelable.Creator
接口的对象。.aidl
文件,声明该 Parcelable 类(如下述 Rect.aidl
文件所示)。如果采用自定义编译方式,请不要把 .aidl
文件加入编译项目。与 C 语言的头文件类似, .aidl
文件不会被编译。AIDL 利用上述方法和成员变量来分解和组装对象。
我们在上述的项目基础上做修改,服务器端:
一、 修改的IRemoteAIDL.aidl文件:
// IRemoteAIDL.aidl package com.huhx.linux; import com.huhx.linux.Person; interface IRemoteAIDL { int add(int a, int b); List<Person> addPerson(in Person person); }
二、 增加Person.aidl文件:
// Person.aidl package com.huhx.linux; parcelable Person;
三、 增加Person.java文件,注意它的位置是com.huhx.linux;
package com.huhx.linux; import android.os.Parcel; import android.os.Parcelable; /** * Created by Linux on 2016/5/9. */ public class Person implements Parcelable { public String username; public String password; public Person(String username, String password) { this.username = username; this.password = password; } protected Person(Parcel in) { username = in.readString(); password = in.readString(); } public static final Creator<Person> CREATOR = new Creator<Person>() { @Override public Person createFromParcel(Parcel in) { return new Person(in); } @Override public Person[] newArray(int size) { return new Person[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(username); dest.writeString(password); } }
这里,之前是放在com.huhx.linux.aidlclient下的,但是处理起来总是报错。不知道是否要求aidl的包名与序列化类的包名一致?
三、 修改RemoteService.java文件
public class RemoteService extends Service { List<Person> persons = new ArrayList<>(); private final static String TAG = "RemoteService"; private IRemoteAIDL.Stub binder = new IRemoteAIDL.Stub() { @Override public int add(int a, int b) throws RemoteException { Log.i(TAG, "a = " + a + ", b = " + b); return a + b; } @Override public List<Person> addPerson(Person person) throws RemoteException { persons.add(person); return persons; } }; @Override public IBinder onBind(Intent intent) { return binder; } }
我们在上述的项目基础上做修改,客户端:
aidl文件以及Person类与服务器端的一样,这里不做说明,在MainActivity中增加测试的方法。
// 添加一个方法 public void addPerson(View view) { Person person1 = new Person("Linux", "123456"); Person person2 = new Person("Huhx", "654321"); int length = 0; try { iRemoteAIDL.addPerson(person1); length = iRemoteAIDL.addPerson(person2).size(); Toast.makeText(MainActivity.this, "list size: " + length, Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } }
标签:
原文地址:http://www.cnblogs.com/huhx/p/baseAIDL1.html