5

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

我的广播接收器

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
}
}

将接收器注册到“app1”

mExportReceiver = new ExportReceiver();
mExportReceiver.setExport(uri -> {});
mContext.registerReceiver(mExportReceiver, ExportReceiver.getIntentFilters());

向“app2”发送广播

override fun onExportFile(fileUri: Uri?) {
    fileUri?.let {
        sendBroadcast(Intent(ExportReceiver.ACTION_EXPORT).apply {
            data = fileUri
        })
    }
}
4

0 回答 0