AIDL(Android Interface Definition Language洞焙,Android接口定義語言)姓赤,它可以用于讓某個Service與多個應用程序組件之間進行跨進程通信院水,從而可以實現(xiàn)多個應用程序共享同一個Service的功能击孩。
Messenger實現(xiàn)跨進程通信焙压,其實也是基于AIDL作為底層結(jié)構(gòu)忆家。Messenger創(chuàng)建的一個消息隊列是在一個單獨的線程中段审,所以服務一次僅處理一個請求全蝶,然而,如果想要服務同時處理多個請求,就需要使用到AIDL抑淫,但是這種情況下就要考慮多線程和線程安全的問題了绷落。
注意:如果只需要IPC(跨進程通信)、不需要多線程使用Messenger即可始苇;如果只需要IPC砌烁、不需要多線程、有多個應用程序則使用Binder即可催式;只有當需要IPC函喉,有多個應用程序,多線程時才使用AIDL荣月。
示例:在客戶端調(diào)用服務端的方法
1管呵、服務端
1)創(chuàng)建AIDL接口,先在app/src/main目錄下創(chuàng)建aidl文件夾哺窄,再在jun.server(程序的包名)包下創(chuàng)建AIDL接口文件IStudent.aidl捐下。
app/src/main/aidl/jun.server/IStudent.aidl
// IStudent.aidl
package jun.server;
// Declare any non-default types here with import statements
interface IStudent {
String getName();
}
2)創(chuàng)建AIDL接口文件之后系統(tǒng)是不會自動幫我們編譯的,需要手動點擊編譯堂氯,之后會生成相應的Java文件蔑担。
app/build/generated/source/aidl/debug/jun.server/IStudent.java
3)創(chuàng)建服務
RemoteService.java
public class RemoteService extends Service {
// 解釋:Stub是繼承Binder的
IStudent.Stub studentBinder = new IStudent.Stub() {
// 獲取學生名字
@Override
public String getName() throws RemoteException {
return "Tom";
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return studentBinder;
}
@Override
public void onDestroy() {
super.onDestroy();
// 服務銷毀前將Binder置空,方便垃圾回收器回收資源咽白。
studentBinder = null;
}
}
4)記得注冊Service
<service android:name=".RemoteService">
<intent-filter>
<action android:name="jun.server.RemoteService"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
2啤握、客戶端
1)直接將服務端的AIDL接口文件拷貝到客戶端,并手動編譯晶框。
app/src/main/aidl/jun.server/IStudent.aidl
注意:包名一樣
2)創(chuàng)建Activity的布局
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="綁定服務"/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解除綁定"/>
<Button
android:id="@+id/invoke_method"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="調(diào)用遠程服務的方法"/>
</LinearLayout>
添加了三個按鈕便于操作排抬。
3)在Activity中綁定遠程服務并調(diào)用它的方法
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private IStudent mIStudent;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 拿到遠程服務的代理
mIStudent = IStudent.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mIStudent = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bind_service).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 設置Intent目標是jun.server包的RemoteService
Intent intent = new Intent("jun.server.RemoteService");
// 設置包名
intent.setPackage("jun.server");
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
Toast.makeText(MainActivity.this, "已綁定服務", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.unbind_service).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (serviceConnection != null) {
unbindService(serviceConnection);
// 解除綁定時需要回收mIStudent連接資源
mIStudent = null;
Toast.makeText(MainActivity.this, "已解除綁定", Toast.LENGTH_SHORT).show();
}
}
});
findViewById(R.id.invoke_method).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIStudent != null) {
try {
String name = mIStudent.getName();
Log.i(TAG, "Name: " + name);
} catch (RemoteException e) {
e.printStackTrace();
}
} else {
Toast.makeText(MainActivity.this, "請先綁定服務", Toast.LENGTH_SHORT).show();
}
}
});
}
}
3、運行
先運行服務端再運行客戶端授段,點擊“綁定服務”按鈕讓客戶端綁定服務端的服務蹲蒲,再點擊“調(diào)用遠程服務的方法”按鈕調(diào)用服務端的getName()方法,最后點擊“解除綁定”按鈕解除綁定侵贵。運行結(jié)果如下:
I/MainActivity: Name: Tom
參考
Android--Service之綁定服務交互
Android Service完全解析届搁,關(guān)于服務你所需知道的一切(下)
墨客網(wǎng):AIDL-小白成長記(視頻教程)