我写这篇文章不想从最基础的介绍开始,我直接上步骤吧.
aidl名字可以自定义也可以默认
basicTypes是自带的,可以删掉,也可以不删,然后把你自己所需的接口写上去
这个就是生成的java文件
package com.howfor.receiver
import android.app.Service
import android.content.Intent
import android.graphics.Bitmap
import android.os.IBinder
import android.util.Log
class MyService : Service() {
var catBinder = object: IReceiverAidl.Stub() {
override fun basicTypes(
anInt: Int,
aLong: Long,
aBoolean: Boolean,
aFloat: Float,
aDouble: Double,
aString: String?
) {
Log.e("TAG","basicTypessetTime===========anInt")
}
override fun setTime(time: Long) {
Log.e("TAG","setTime===========$time")
}
override fun reboot() {
Log.e("TAG","reboot===========")
adwApi!!.Reboot()
}
override fun shutdown() {
Log.e("TAG","shutdown===========")
adwApi!!.ShutDown()
}
override fun install(fullPath: String?) {
Log.e("TAG","install===========$fullPath")
}
override fun beat(aBoolean: Boolean) {
Log.e("TAG","beat===========$aBoolean")
}
override fun setPower(power: MutableList?) {
Log.e("TAG","setPower===========${power!!.size}")
}
override fun updateFirmware(localFile: String?) {
Log.e("TAG","updateFirmware===========${localFile}")
}
override fun getDeviceId(): String {
Log.e("TAG","getDeviceId===========")
return ""
}
override fun getName(): Array {
Log.e("TAG","getName()===========")
val arrayEmpty = emptyArray()
return arrayEmpty
}
override fun getPath(): Array {
Log.e("TAG","getPath()===========")
val arrayEmpty = emptyArray()
return arrayEmpty
}
override fun shouldClean(path: String?): Boolean {
Log.e("TAG","shouldClean()===========")
return true
}
override fun getBitmap(): Bitmap {
Log.e("TAG","getBitmap()===========")
var bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
return bitmap
}
override fun getVersion(): String {
Log.e("TAG","getVersion()===========")
return ""
}
}
override fun onBind(intent: Intent): IBinder {
return catBinder
}
}
package com.example.aidlkhute
import android.annotation.SuppressLint
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.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.howfor.receiver.IReceiverAidl
class MainActivity : AppCompatActivity() {
var mIReceiverAidl: IReceiverAidl? = null
var mConnections = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
Log.e("TAG", "onServiceConnected")
//注释3:
mIReceiverAidl = IReceiverAidl.Stub.asInterface(service)
}
override fun onServiceDisconnected(name: ComponentName?) {
Log.e("TAG", "onServiceDisconnected")
mIReceiverAidl = null
}
}
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent()
intent.setPackage("com.howfor.receiver")
intent.action = "com.howfor.receiver.service.ReceiverService"
try {
var se = bindService(intent, mConnections, Context.BIND_AUTO_CREATE)
Log.e("TAG", "se=========$se")
} catch (e: RemoteException) {
Log.e("TAG", "e=========${e.message}")
}
}
override fun onDestroy() {
super.onDestroy()
unbindService(mConnections)
}
}
添加两个按钮使用里面的接口
findViewById
先运行服务端,可以不用做任何操作,然后再运行客户端,点击对应的接口,就会打印出对应的log:
se=========true //代表服务绑定成功
reboot=========== //点击第一个按钮打印
shutdown=========== //点击第二个按钮打印
还有个小问题:在高版本(targetSdk 33)上会出现绑定失败的情况,然后查看了一下,在安卓11以上,google对系统做了一些操作,具体的可以自行百度:解决方案,在客户端添加红框中的内容即可