android activity pass data to accessibilityservice 数据传递

不同类型的 service 传递数据的方式不同,accessibilityservice 运行在独立进程,且被系统接管,比较特别

在 AccessibilityService 的 onCreate 内加一个 BroadcastReceiver

val broadcast = object:BroadcastReceiver(){
        override fun onReceive(context: Context?, intent: Intent?) {
            val what = intent?.getStringExtra("hello",  "dlrow")!!
        }
}
registerReceiver(broadcast, IntentFilter("hangj.cnblogs.com"))

onUnbind 里

unregisterReceiver(broadcast)

在 Activity 内 sendBroadcast

sendBroadcast(Intent().apply {
    action="hangj.cnblogs.com"
    putExtra("hello", "world")
})

还可以用 socket

原文地址:https://www.cnblogs.com/hangj/p/13744210.html