AIDL是Android多進程通訊方式一種。
如要使用 AIDL 創(chuàng)建綁定服務,請執(zhí)行以下步驟:
-
創(chuàng)建 .aidl 文件
此文件定義帶有方法簽名的編程接口。
-
實現(xiàn)接口
Android SDK 工具會基于您的?
.aidl
?文件,使用 Java 編程語言生成接口。此接口擁有一個名為?Stub
?的內(nèi)部抽象類,用于擴展?Binder
?類并實現(xiàn) AIDL 接口中的方法。您必須擴展?Stub
?類并實現(xiàn)這些方法。 -
向客戶端公開接口
實現(xiàn)?
Service
?并重寫?onBind()
,從而返回?Stub
?類的實現(xiàn)。
以下是?.aidl
?文件示例
// IMyAidlInterface.aidl
package com.jason.aidlserver;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* 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);
String getInfo() ;
}
服務端實現(xiàn)接口 :?
功能實現(xiàn),由客戶端發(fā)送數(shù)據(jù)過來,簡單保存一下aString。然后客戶端讀取時候返回。?
class AidlServerService :Service(){
private var aS :String? = "empty"
private val mBinder = object: IMyAidlInterface.Stub (){
override fun basicTypes(
anInt: Int,
aLong: Long,
aBoolean: Boolean,
aFloat: Float,
aDouble: Double,
aString: String?
) {
aS = aString
}
override fun getInfo(): String {
return " RemoteService保存數(shù)據(jù) => $aS"
}
}
override fun onBind(intent: Intent?): IBinder {
return mBinder
}
}
然后我們在注冊清單 AndroidManifest給server加上權(quán)限。
<permission android:name="jason.aidl" android:protectionLevel="normal" /> <uses-permission android:name="jason.aidl" />
<service android:name=".AidlServerService" android:exported="true" android:permission="jason.aidl"> <intent-filter> <action android:name="jason.aidl.action" /> </intent-filter> </service>
這樣server端服務已經(jīng)開發(fā)好了,只需要在Activity把這個服務啟動即可。?
客戶端集成接口:?
客戶端還必須擁有接口類的訪問權(quán)限,因此如果客戶端和服務在不同應用內(nèi),則客戶端應用的?src/
?目錄內(nèi)必須包含?.aidl
?文件(該文件會生成?android.os.Binder
?接口,進而為客戶端提供 AIDL 方法的訪問權(quán)限)的副本。?
代碼示例:??
class MainActivity : AppCompatActivity() {
private lateinit var tvMsg :TextView
private var autoMex :Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvMsg = findViewById(R.id.tv_msg)
findViewById<Button>(R.id.btn_connect).setOnClickListener {
val intent = Intent( "jason.aidl.action")
intent.setClassName("com.jason.aidlserver","com.jason.aidlserver.AidlServerService")
bindService(intent,connect, Context.BIND_AUTO_CREATE)
}
findViewById<Button>(R.id.btn_send).setOnClickListener {
remoteService?.basicTypes(1,2L,true,1.5F,2.5,"haha ${autoMex ++}")
}
findViewById<Button>(R.id.btn_get).setOnClickListener {
tvMsg.text = remoteService?.info
}
}
private var remoteService :IMyAidlInterface ? = null
private val connect = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
remoteService = IMyAidlInterface.Stub.asInterface(service)
tvMsg.text = "onServiceConnected"
}
override fun onServiceDisconnected(name: ComponentName?) {
remoteService= null
tvMsg.text = "onServiceDisconnected"
}
}
override fun onDestroy() {
unbindService(connect)
super.onDestroy()
}
}
因為我們給server端記上了權(quán)限,那么客戶端需要把對應權(quán)限加上 。否者會權(quán)限錯誤無法連接到遠程服務。?
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jason.aidlclient"> <!--sdk 30后需要加上 --> <queries> <package android:name="com.jason.aidlserver"/> </queries> <!--加上服務權(quán)限 --> <uses-permission android:name="jason.aidl"/>
展示效果:?
通過 IPC 傳遞對象
?可以通過 IPC 接口,將某個類從一個進程發(fā)送至另一個進程。不過,您必須確保 IPC 通道的另一端可使用該類的代碼,并且該類必須支持?Parcelable
?接口。支持?Parcelable
?接口很重要,因為 Android 系統(tǒng)能通過該接口將對象分解成可編組至各進程的原語。
如要創(chuàng)建支持?Parcelable
?協(xié)議的類,您必須執(zhí)行以下操作:
- 讓您的類實現(xiàn)?
Parcelable
?接口。 - 實現(xiàn)?
writeToParcel
,它會獲取對象的當前狀態(tài)并將其寫入?Parcel
。 - 為您的類添加?
CREATOR
?靜態(tài)字段,該字段是實現(xiàn)?Parcelable.Creator
?接口的對象。 - 最后,創(chuàng)建聲明 Parcelable 類的?
.aidl
?文件。如果您使用的是自定義編譯進程,請勿在您的構(gòu)建中添加?
.aidl
?文件。此?.aidl
?文件與 C 語言中的頭文件類似,并未經(jīng)過編譯。
AIDL 會在其生成的代碼中使用這些方法和字段,以對您的對象進行編組和解編。文章來源:http://www.zghlxwxcb.cn/news/detail-703146.html
AIDL官方文檔指南:?Android 接口定義語言 (AIDL) ?|? Android 開發(fā)者 ?|? Android Developershttps://developer.android.google.cn/guide/components/aidl?hl=zh-cn#Expose文章來源地址http://www.zghlxwxcb.cn/news/detail-703146.html
到了這里,關于Android AIDL基本使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!