android.intent.action.MY_PACKAGE_REPLACED 不工作

标签 android kotlin android-intent broadcastreceiver intentfilter

我似乎无法捕捉到它,而且我也看不到它在 Logcat 中发送。来自评论ACTION_MY_PACKAGE_REPLACED not received ,看起来 MY_PACKAGE_REPLACED 应该是 API22 所需要的。

我错过了什么?

谢谢。

来自 AndroidManifest.xml 的 fragment

    <receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver>

启动接收器

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.BOOT_COMPLETED" ||
                intent.action == "android.intent.action.MY_PACKAGE_REPLACED") {
            Log.d(TAG, "Caught BOOT_COMPLETED or PACKAGE_REPLACED action!")
            GlobalScope.launch(Dispatchers.IO) {
                ...
            }
        }
    }
}

最佳答案

经过进一步调查,如果从图片中删除 Android Studio (AS),此方法可以正常工作;用它来构建 APK,或许还可以查看 Logcat,仅此而已。如果我只从命令行/终端安装/替换应用程序,并且不从 AS 运行应用程序和相关应用程序,这将按预期工作。在我的例子中,因为我经常从 AS 安装/运行,所以我不得不执行以下两次并且 android.intent.action.MY_PACKAGE_REPLACED 被第二次捕获:

adb -s emulator-5554 install -r app-debug.apk

我再说一遍,在这方面,从 Android Studio 运行该应用程序时,android.intent.action.MY_PACKAGE_REPLACED 有几个问题,遗憾的是,我花了几个小时来解决这个问题!

关于android.intent.action.MY_PACKAGE_REPLACED 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64576768/

相关文章:

android - Unity3D 和安卓 : Difference between "UnityMain" and "main" threads?

android - 为什么 Android WebChromeClient 的 onCreateWindow 方法从未被调用过?

kotlin - 从 Kotlin 中的 Map 获取具有最大值的条目

java - 两部手机之间的接近检测(Android)

kotlin - 为什么CoroutineScope.launch和Coroutine.async是扩展函数而不是CoroutineScope的成员函数?

android - 使用数组适配器的微调器实现

android - 我可以检查某个应用程序的通知是否已打开吗?

android - 在 Dialer 中的调用断开连接以 ACTION_CALL 开始后,确保控制返回到调用 Activity 的任何方法

android - onResume() 是在 Intent 完成后调用吗?

android - 为 GridView 的每个按钮打开一个新 Activity