AIDL profile
What is the AIDL
AIDL is one of the Inter-Process Communication (IPC) modes in Android. AIDL is the abbreviation of Android Interface Definition Language
What does AIDL do
For Xiaobai, the purpose of AIDL is to allow you to bind a service of another APP to your APP, so that your APP can interact with other apps.
When is AIDL used
AIDL is better suited for interprocess communication between multiple applications with multiple threads
Binder is better suited for single-threaded interprocess communication between multiple applications
Messenger is better suited for use in single-threaded, single-process applications with only interprocess communication
Use AIDL
Create an AIDL file
First of all, there is the New >Folder–>AIDL Folder on the project side of Android Studio
aidl
java
aidl
Data types supported by AIDL
Android development network has written
All primitive types in the Java programming language (such as int, long, char, boolean, and so on)
String
CharSequence
List
Map
Copy the code
Although the official documents indicate all Java basic data types are supported, but in fact short doesn’t support, if short is contained in the data type, the compiler will report the following ERROR: ERROR: XXX/IMyAidlInterface aidl: 43-15.38: Failed to resolve ‘short’
Note that Map cannot write generics. If you write generics, the following error will be reported at compile time: aidl E 08-31 17:11:09 17172 8244311 type_java.cpp:357] Don’t know how to create a Map
container.
A List can be written to generics, but only those of the above mentioned data types can be written to generics. An Integer is not supported. XXX/IMyAidlInterface. Aidl: 16.63-70: Failed to resolve ‘Integer’
Custom datatypes that implement Parcelable are also supported, but you must create an.aidl file declaring your Parcelable class, for example
package top.xuqingquan.aidltest;
parcelable User;
Copy the code
For all non-basic data types, a directional tag is required to specify which direction the data is going in, out, and inout
Here is the full AIDL file
// IMyAidlInterface.aidl
package top.xuqingquan.aidltest;
import java.util.List;
import java.util.Map;
import top.xuqingquan.aidltest.User;
interface IMyAidlInterface {
List<User> basicTypes(byte aByte,/*short aShort,*/ int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble,char aChar, String aString,in List<String> aList,in Map aMap,CharSequence aCharSequence,in User user);
}
Copy the code
Server-side implementation
Create a new Service. The onBind method returns an interface that implements aiDL.
package top.xuqingquan.aidltest
import android.app.Service
import android.content.Intent
import android.os.IBinder
class ITestAidlService : Service() {
private lateinit varusers: MutableList<User? >override fun onBind(p0: Intent?).: IBinder {
users = arrayListOf()
return iBinder
}
private val iBinder: IBinder = object : IMyAidlInterface.Stub() {
override fun basicTypes(
aByte: Byte,
anInt: Int,
aLong: Long,
aBoolean: Boolean,
aFloat: Float,
aDouble: Double,
aChar: Char,
aString: String? , aList:MutableList<String>? , aMap:MutableMap<Any? , Any? >? , aCharSequence:CharSequence? , user:User?).: List<User? > { users.add(user)return users
}
}
}
Copy the code
Client-side implementation
The client needs to make a copy of the entire aidl directory on the server side. For the User class on the server side, it also needs to define the same package directory as the server side. The rest only needs to bindService with bindService. ServiceConnection’s onServiceConnected node gets the remote AIDL, which you can use to call the related methods
package top.xuqingquan.aidlclienttest
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
import kotlinx.android.synthetic.main.activity_main.*
import top.xuqingquan.aidltest.IMyAidlInterface
import top.xuqingquan.aidltest.User
class MainActivity : AppCompatActivity() {
private lateinit var iTestAidl: IMyAidlInterface
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get to the server
val intent = Intent()
//Android 5.1 starts with an explicit intent to start a service
intent.component =
ComponentName("top.xuqingquan.aidltest"."top.xuqingquan.aidltest.ITestAidlService")
bindService(intent, object : ServiceConnection {
override fun onServiceDisconnected(p0: ComponentName?). {
println("onServiceDisconnected")}override fun onServiceConnected(p0: ComponentName? , p1:IBinder?). {
println("onServiceConnected")
// Get remote to aiDL
iTestAidl = IMyAidlInterface.Stub.asInterface(p1)
}
}, Context.BIND_AUTO_CREATE)
tv_result.setOnClickListener {
try {
val list = iTestAidl.basicTypes(
0.0.0.false.0f, 0.0.'a'."", emptyList(),
emptyMap<Any, Any>(), "", User("test".11)
)
tv_result.text = "${list.size}-$list"
} catch (e: RemoteException) {
e.printStackTrace()
}
}
}
}
Copy the code
Finally, AIDL document attached to the address: developer. The android, Google. Cn/guide/compo…