AIDL(Android Interface Definition Language)指的就是接口定義語(yǔ)言,通過(guò)它可以讓客戶(hù)端與服務(wù)端在進(jìn)程間使用共同認(rèn)可的編程接口來(lái)進(jìn)行通信
AIDL使用的步驟相對(duì)較多榴鼎,主要總結(jié)為三個(gè)基本步驟:
- 創(chuàng)建AIDL接口
- 根據(jù)AIDL創(chuàng)建遠(yuǎn)程Service服務(wù)
- 綁定遠(yuǎn)程Service服務(wù)
-
創(chuàng)建AIDL接口
-
創(chuàng)建AIDL接口
在工程目錄中蕉毯,依次app ->new->AIDL张漂,即可創(chuàng)建接口如下:
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接口支持的數(shù)據(jù)類(lèi)型String
误褪、CharSequence
悉患、List
亭螟、Map
以及自定義的數(shù)據(jù)類(lèi)型(需要實(shí)現(xiàn)Parcelable
接口) -
在AIDL包下創(chuàng)建自定義的數(shù)據(jù)類(lèi)型
新建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]; } }; }
同時(shí),,創(chuàng)建PigBean.aidl,聲明該類(lèi)實(shí)現(xiàn)了parcelable接口,如:
package com.example.juny.devofexploration; parcelable PigBean;
修改 創(chuàng)建AIDL接口方法如下:
interface IMyAidlInterface { void addPig(in PigBean pig); List<PigBean> getPigList(); }
- 根據(jù)aidl文件生成java接口文件
這個(gè)步驟Android Studio已經(jīng)幫我們集成好了,只需要點(diǎn)擊 Build -> Make Project预烙,或者點(diǎn)擊AS上的那個(gè)小錘子圖標(biāo)就可以墨微,構(gòu)建完后將會(huì)自動(dòng)根據(jù)我們定義的
IMyAidlInterface.aidl
文件生成IMyAidlInterface.java
接口類(lèi),可以在build/generated/source/aidl/debug/
路徑下找到這個(gè)類(lèi)
-
-
根據(jù)AIDL接口默伍,遠(yuǎn)程服務(wù)Service實(shí)現(xiàn)
`mIBinder`對(duì)象實(shí)例化了`IMyAidlInterface.Stub`欢嘿,并在回調(diào)接口中實(shí)現(xiàn)了最終的處理邏輯當(dāng)與客戶(hù)端綁定時(shí),會(huì)觸發(fā)onBind()方法也糊,并返回一個(gè)Binder對(duì)象給客戶(hù)端使用炼蹦,客戶(hù)端就可以通過(guò)這個(gè)類(lèi)調(diào)用服務(wù)里實(shí)現(xiàn)好的接口方法:
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屬性指定其運(yùn)行在新的進(jìn)程中:<service android:name=".MyAidlService" android:process=":process"/>
-
客戶(hù)端綁定遠(yuǎn)程服務(wù)
創(chuàng)建連接對(duì)象
mServiceConnection
使用
Intent
的 bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE) 方法進(jìn)行連接-
通過(guò)
IMyAidlInterface
對(duì)象調(diào)用接口方法整體代碼如下:
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(); } } }); } }