AIDL(Android Interface Definition Language)指的就是接口定义语言,通过它可以让客户端与服务端在进程间使用共同认可的编程接口来进行通信
AIDL使用的步骤相对较多,主要总结为三个基本步骤:
- 创建AIDL接口
- 根据AIDL创建远程Service服务
- 绑定远程Service服务
-
创建AIDL接口
-
创建AIDL接口
在工程目录中,依次app ->new->AIDL,即可创建接口如下:
interface IMyAidlInterface2 { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); }
不需要的
basicTypes
方法可以删掉,AIDL接口支持的数据类型String
、CharSequence
、List
、Map
以及自定义的数据类型(需要实现Parcelable
接口) -
在AIDL包下创建自定义的数据类型
新建Pigbean.java,如下:
package com.example.juny.devofexploration; import android.os.Parcel; import android.os.Parcelable; /** * @author ChenRunFang */ public class PigBean implements Parcelable { public String name; public String weight; protected PigBean(String name, String weight) { this.name = name; this.weight = weight; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(weight); } @Override public int describeContents() { return 0; } public static final Creator<PigBean> CREATOR = new Creator<PigBean>() { @Override public PigBean createFromParcel(Parcel in) { return new PigBean(in.readString(),in.readString()); } @Override public PigBean[] newArray(int size) { return new PigBean[size]; } }; }
同时,,创建PigBean.aidl,声明该类实现了parcelable接口,如:
package com.example.juny.devofexploration; parcelable PigBean;
修改 创建AIDL接口方法如下:
interface IMyAidlInterface { void addPig(in PigBean pig); List<PigBean> getPigList(); }
- 根据aidl文件生成java接口文件
这个步骤Android Studio已经帮我们集成好了,只需要点击 Build -> Make Project,或者点击AS上的那个小锤子图标就可以,构建完后将会自动根据我们定义的
IMyAidlInterface.aidl
文件生成IMyAidlInterface.java
接口类,可以在build/generated/source/aidl/debug/
路径下找到这个类
-
-
根据AIDL接口,远程服务Service实现
`mIBinder`对象实例化了`IMyAidlInterface.Stub`,并在回调接口中实现了最终的处理逻辑当与客户端绑定时,会触发onBind()方法,并返回一个Binder对象给客户端使用,客户端就可以通过这个类调用服务里实现好的接口方法:
package com.example.juny.devofexploration; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import java.util.ArrayList; import java.util.List; /** * @author ChenRunFang */ public class MyAidlService extends Service { private List<PigBean> mPigBeans; public MyAidlService() { } private IBinder mIBinder = new IMyAidlInterface.Stub() { @Override public void addPig(PigBean pig) throws RemoteException { mPigBeans.add(pig); } @Override public List<PigBean> getPigList() throws RemoteException { return mPigBeans; } }; @Override public IBinder onBind(Intent intent) { mPigBeans = new ArrayList<>(); return mIBinder; } }
记得在
AndroidManifest
中声明, 并使用android:process属性指定其运行在新的进程中:<service android:name=".MyAidlService" android:process=":process"/>
-
客户端绑定远程服务
创建连接对象
mServiceConnection
使用
Intent
的 bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE) 方法进行连接-
通过
IMyAidlInterface
对象调用接口方法整体代码如下:
package com.example.juny.devofexploration; 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.Button; import android.widget.Toast; import java.util.List; /** * @author ChenRunFang */ public class MainActivity extends AppCompatActivity { private IMyAidlInterface mIMyAidlInterface; private PigBean mPigBean; private Button mBindBtn; private Button mCommunicationBtn; private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { mIMyAidlInterface = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBindBtn = findViewById(R.id.btn_bind); mCommunicationBtn = findViewById(R.id.btn_communication); mBindBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), MyAidlService.class); bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); } }); mCommunicationBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPigBean = new PigBean("zu", "100"); try { mIMyAidlInterface.addPig(mPigBean); List<PigBean> mPigList = mIMyAidlInterface.getPigList(); Toast.makeText(MainActivity.this, "zhu = " + mPigList.get(0).name + mPigList.get(0).weight, Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } }); } }