首页 > 解决方案 > android.intent.action.MY_PACKAGE_REPLACED 不工作

问题描述

我似乎无法捕捉到这一点,我也看不到它在 Logcat 中发送。通过查看ACTION_MY_PACKAGE_REPLACED not received,看来 MY_PACKAGE_REPLACED 应该是我对 API22 所需要的。

我错过了什么?

谢谢你。

来自 AndroidManifest.xml 的片段

    <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) {
                ...
            }
        }
    }
}

标签: androidkotlinandroid-intentbroadcastreceiverintentfilter

解决方案


经过进一步调查,如果从图片中删除 Android Studio (AS),这将正常工作;使用它来构建 APK,也许还可以查看 Logcat,但仅此而已。如果我只从命令行/终端安装/替换应用程序,而不是从 AS 运行应用程序和相关应用程序,这将按预期工作。就我而言,由于我经常从 AS 安装/运行,因此我必须执行以下 TWICE 并且 android.intent.action.MY_PACKAGE_REPLACED 第二次被捕获:

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

我再说一遍,从 Android Studio 运行应用程序,在这方面,与 android.intent.action.MY_PACKAGE_REPLACED 相混淆,遗憾的是,我浪费了几个小时来弄清楚这一点!


推荐阅读