Android onReceive()方法之后的应用程序升级永远不会被调用

Android onReceive()方法之后的应用程序升级永远不会被调用,android,Android,我想在升级后启动我的应用程序。 我同时使用了android.intent.action.MY_PACKAGE_替换和android.intent.action.PACKAGE_替换操作,但它从未调用我的onReceive方法。 我的应用程序的最小SDK版本是21。 此应用不包含活动类。仅包含服务。 我创建这个应用程序是为了在这个设备上启动我的另一个android应用程序 这是清单代码 <application android:name=".DeviceAdminApplic

我想在升级后启动我的应用程序。 我同时使用了android.intent.action.MY_PACKAGE_替换和android.intent.action.PACKAGE_替换操作,但它从未调用我的onReceive方法。 我的应用程序的最小SDK版本是21。 此应用不包含活动类。仅包含服务。 我创建这个应用程序是为了在这个设备上启动我的另一个android应用程序

这是清单代码

<application
        android:name=".DeviceAdminApplication"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name=".SchedulerJobService"
            android:permission="android.permission.BIND_JOB_SERVICE" />
        <receiver
            android:name=".AdminBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </receiver>
        <receiver android:name=".AppInstalledReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
                <data android:scheme="package" android:path="com.pnct.agent"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <data android:scheme="package" android:path="com.pnct.agent"/>
            </intent-filter>
        </receiver>
    </application>
如果我安装了任何其他应用程序,则调用onReceive方法。 请帮我解决这个问题 谢谢。

来自

清单申报接收人

注意:如果您的应用程序目标API级别为26或更高,则无法使用 声明隐式广播(广播)接收器的清单 不专门针对你的应用),除了一些 不受该限制的广播。在大多数情况下,你 可以改为使用计划作业

中也提到了这些变化

应用程序无法使用其清单注册大多数隐式应用程序 广播(即不专门针对 应用程序)

解决方案 您可以执行以下操作之一

  • 您可以在活动或服务中注册,而不是在清单中注册。看
  • 一个人在奥利奥牛轧糖设备的舱单上做了一个注册广播接收器的把戏。看
  • 更新 看

    行动\u我的\u包\u被替换

    广播操作:已安装应用程序的新版本 在现有的基础上。这只会发送到已创建的应用程序 替换。它不包含任何附加数据;要接受它,只需 为此操作使用意图筛选器


    您的旧应用程序将获得此广播。因此,请检查旧应用程序是否有广播接收器。

    替换的
    操作\u我的\u软件包的文档说明:

    您的应用程序的新版本已在现有服务器上安装 一个。这只发送到被替换的应用程序。是的 不包含任何附加数据;要接受它,只需使用意图 此操作的筛选器

    这意味着旧应用程序应该接收广播,而不是新应用程序。所以至少你不能用它来启动应用程序

    如果要手动或通过代码更新apk,请选中。

    SDK路径\平台工具文件夹,我的意思是“C:\AndroidStudio\U SDK\平台工具”

    • 运行命令提示符并执行cd C:\AndroidStudio_SDK\platform tools

    • 之后,执行adb shell am广播-一个android.intent.action.MY_包被替换

    此方法将运行UpdateReceiver或您命名的任何类。我建议添加数据并控制它。如果数据存在,则调用它


    命令提示符->shell am broadcast-a android.intent.action.MY_PACKAGE_替换为-n com.example.myappname/.UpdateReceiver


    此方法直接运行到您的接收器

    ,感谢您的回复。但我的应用程序的目标sdk版本也是21。如果我安装了任何其他应用程序,则调用onReceive方法,如果我升级此应用程序,则仅调用onReceive方法。如果相同的代码以前有效,但现在无效,则我们无法帮助您,你必须找出你所做的更改。否。我的意思是,如果我安装了任何其他应用程序,那么onReceive方法仍然会被调用。问题是,如果我升级我的应用程序,那么只有onReceive方法不会在你升级应用程序时调用,然后你的应用程序在更新过程中被删除,那么这将如何调用?@rockKah你自己说这只发送到被替换的应用程序。意味着旧应用程序应在更换前接收,而不是新应用程序。或者至少这是我从文件中得到的。
    public class AppInstalledReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(Constants.LOGTAG, "inside on receive");
            Log.d(Constants.LOGTAG, "action:"+intent.getAction());
            if(intent.getAction().equalsIgnoreCase("android.intent.action.MY_PACKAGE_REPLACED") ||
                    (intent.getAction().equalsIgnoreCase("android.intent.action.PACKAGE_REPLACED"))){
                Log.d(Constants.LOGTAG, "package replaced");
            }}