小编idF*_*ida的帖子

Android sendBroadcast到其他模块

我有一个主应用程序模块“app1”和一个以“process:SecondActivity”开头的第二个模块“app2”。我需要使用广播接收器从 SecondActivity 发送消息,但该消息不在 onRecive 中。广播接收器位于模块“tools”中,模块“app1”和“app2”依赖于模块“tools”。

我的广播接收器

class ExportReceiver : BroadcastReceiver() {

companion object {

    const val ACTION_EXPORT = "ACTION_EXPORT"

    @JvmStatic
    fun getIntentFilters() = IntentFilter(ACTION_EXPORT)

}

@FunctionalInterface
interface OnExport {
    fun onExport(uri: Uri)
}

private var mExport : OnExport? = null

override fun onReceive(context: Context?, intent: Intent?) {
    intent?.let {
        if (it.action == ACTION_EXPORT) {
            it.data?.let { uri ->
                mExport?.onExport(uri)
            }
        }
    }
}

fun setExport(export: OnExport) {
    mExport = export
}
}
Run Code Online (Sandbox Code Playgroud)

将接收器注册到“app1”

mExportReceiver = new ExportReceiver();
mExportReceiver.setExport(uri -> {}); …
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver android-broadcast android-module

5
推荐指数
0
解决办法
530
查看次数