美文网首页
Capacitor 框架下解决One of RECEIVER_E

Capacitor 框架下解决One of RECEIVER_E

作者: 齐格的修为课 | 来源:发表于2025-04-21 18:16 被阅读0次

背景

在适配我们星光 MES APP在手机端的时候,星光 APP启动报错:

Caused by: java.lang.SecurityException: com.sancaiwulian.starmes: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

问题分析

这个问题是在 Android 14 版本以上才会出现,网上有讨论
https://stackoverflow.com/questions/77235063/one-of-receiver-exported-or-receiver-not-exported-should-be-specified-when-a-rec

image.png

出现这个问题是我们星光 APP需要监听到扫码抢的事件通知,所以,我们采用了 capacitor-intents 这个插件。看它的源码:

this.getContext().registerReceiver(receiverMap.get(callBackID), ifilt);

可以看到,它在注册监听的时候没有添加 RECEIVER_EXPORTED (表示可以监听其他 APP 的事件通知) 或 RECEIVER_NOT_EXPORTED (表示只能监听自己 APP 的事件通知)参数。

解决方法

看已经有人在自己的 fork 的仓库里修复了这个问题:

https://github.com/prathap-reddy-shipsy/capacitor-android-intents/blob/main/android/src/main/java/com/itmikes/capacitorintents/CapacitorIntents.java

但他没有发布最新的包,所以,你自己也用不了。

看来只能自己 fork 一份,重新发一个版本就好。@nantajs/capacitor-intents

使用的时候替换下原先的包(将包capacitor-android-intents替换为@nanta/capacitor-intents),其他都不需要变。

npm install @nanta/capacitor-intents
npx cap sync

https://www.npmjs.com/package/@nanta/capacitor-intents

改动源码:
https://github.com/nantajs/capacitor-intents/blob/main/packages/capacitor-plugin/android/src/main/java/com/itmikes/capacitorintents/CapacitorIntents.java

image.png

相关文章

网友评论

      本文标题:Capacitor 框架下解决One of RECEIVER_E

      本文链接:https://www.haomeiwen.com/subject/ibwbbjtx.html