android.intent.action.MY_PACKAGE_REPLACED 不工作

android.intent.action.MY_PACKAGE_REPLACED Not Working

我似乎无法捕捉到它,而且我也看不到它在 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) {
                ...
            }
        }
    }
}

经进一步调查,如果从图片中删除 Android Studio (AS),此方法将正常工作;用它来构建 APK,或许还可以查看 Logcat,仅此而已。如果我只 install/replace 来自命令 line/Terminal 的应用程序,而不 运行 来自 AS 的应用程序和相关应用程序,这将按预期工作。就我而言,因为我经常从 AS install/Run,所以我不得不执行以下两次并且 android.intent.action.MY_PACKAGE_REPLACED 第二次被抓到:

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

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