1.使用Intent传递数据
在跳转的Intent中携带数据,常规使用方法,不再举例。
参考360插件化方案RePlugin初体验 中跳转intent,向intent中放入数据进行传递,也可使用startActivityForResult传递数据并跳转到插件,再通过setResult从插件传递数据给宿主。
2.使用广播
新建TestPluginReceiver.kt
class TestPluginReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context, "已经收到信息", Toast.LENGTH_SHORT).show()
}
}
宿主中注册:
//声明
private lateinit var pluginReceiver: TestPluginReceiver
//注册
pluginReceiver = TestPluginReceiver()
registerReceiver(pluginReceiver, IntentFilter("com.test.qby.receiver"))
//别忘记在onDestroy中注销
unregisterReceiver(pluginReceiver)
插件中发送广播
sendBroadcast(Intent("com.test.qby.receiver"))
3.使用Messenger(仿照应用间通信方式)
把宿主当作服务端,新建MessengerService.kt
class MessengerService : Service() {
companion object {
private val TAG = "PluginActivity"
}
private val mMessenger = Messenger(MessengerHandler())
override fun onBind(intent: Intent): IBinder? {
return mMessenger.binder
}
private class MessengerHandler : Handler() {
override fun handleMessage(msg: Message) {
when (msg.what) {
1 -> {
Log.e(TAG, "宿主得到信息")
val data = msg.data
val msgContent = data.getString("msg")
Log.e(TAG, "插件发来的信息是" + msgContent!!)
// 回复客户端
val client = msg.replyTo
val replyData = Bundle()
replyData.putString("reply", "插件发来的信息<$msgContent>,宿主已收到")
val replyMsg = Message.obtain()
replyMsg.what = 0
replyMsg.data = replyData
try {
client.send(replyMsg)
} catch (e: RemoteException) {
Log.e(TAG, "", e)
}
}
else -> super.handleMessage(msg)
}
}
}
}
宿主清单文件中注册
<service android:name=".service.MessengerService"
android:exported="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.test.qby.myapplication.service.MessengerService" />
</intent-filter>
</service>
在插件中绑定服务,发送信息,并获取结果
//声明,初始化
companion object {
val TAG: String = "PluginActivity"
}
var mService:Messenger?= null
var mMessenger:Messenger = Messenger(MessengerHandler())
//获取信息的handler
@Suppress("UNUSED_EXPRESSION")
class MessengerHandler: Handler() {
override fun handleMessage(msg: Message) {
Log.e(TAG, "插件得到信息")
when (msg.what) {
0 -> {
val data: Bundle = msg.data
Log.e(TAG, "" + data.getString("reply"))
}
else -> ""
}
}
}
//服务连接
private val mConnection: ServiceConnection = object: ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
mService = Messenger(service)
// 向服务端发送数据
try {
val msg = Message.obtain()
msg.what = 1
val data = Bundle()
data.putString("msg", "能看到我吗?")
msg.data = data
// 设置服务端回复的Messenger
msg.replyTo = mMessenger
mService!!.send(msg)
} catch (e:RemoteException) {
Log.e(TAG, "", e)
}
}
}
// 绑定 Service
val service = Intent("com.test.qby.myapplication.service.MessengerService")
service.component = ComponentName("com.test.qby.myapplication", "com.test.qby.myapplication.service.MessengerService")
RePlugin.getHostContext().bindService(service, mConnection, Service.BIND_AUTO_CREATE)
//onDestroy中断开连接
RePlugin.getHostContext().unbindService(mConnection)
4.使用AIDL
参考360插件化方案RePlugin学习笔记-插件使用宿主中的类中 使用场景3
5.使用EventBus
宿主项目app下build.gradle中添加依赖
compile 'org.greenrobot:eventbus:3.1.1'
新建EventBus事件类MessageEvent.kt
class MessageEvent(var message: String?) {
//发出信息的方法
fun sendEventBus() {
EventBus.getDefault().post(this)
}
}
宿主中注册、执行事件、注销
//注册
EventBus.getDefault().register(this)
//执行事件的方法,doEvent为自定义,命名随意,参数与post参数一致即可
@Subscribe(threadMode = ThreadMode.MAIN)
fun doEvent(messageEvent: MessageEvent?) {
Toast.makeText(this, messageEvent!!.message,Toast.LENGTH_SHORT).show()
}
//注销
if(EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
插件中调用,发送信息的方法
//获取宿主类加载器
val hostClassLoader = RePlugin.getHostClassLoader()
//获取MessageEvent类
val msgEvent = hostClassLoader.loadClass("com.test.qby.myapplication.pojo.MessageEvent")
//得到有参构造方法
val constructor = msgEvent.getConstructor(String::class.java)
//调用有参构造,传递参数
val eventBusInfo = constructor.newInstance("使用EventBus传递信息")
//获取发送方法
val method = msgEvent.getDeclaredMethod("sendEventBus")
//调用发送方法,发出信息
method.invoke(eventBusInfo)
通信方式的选择还是要根据具体的使用场景来决定。
以上仅个人学习记录,如有疏漏或谬误,欢迎留言交流!