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

时间:2018-09-24 08:39:40

标签: android

我想在升级后启动我的应用程序。 我同时使用了android.intent.action.MY_PACKAGE_REPLACED和android.intent.action.PACKAGE_REPLACED操作,但是它从未调用过我的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方法

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");
        }}

如果我安装了其他应用程序,则会调用onReceive方法。 请帮我解决这个问题 谢谢。

3 个答案:

答案 0 :(得分:0)

来自Android Broadcast Documentation

  

舱单声明的收件人

     

注意:如果您的应用定位到API级别26或更高级别,则不能使用   清单以声明用于隐式广播的接收方(广播   并不专门针对您的应用),除了一些隐式   不受此限制的广播。在大多数情况下,   可以改为使用预定的作业。

Android Oreo behaviour changes中也提到了这些更改。

  

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

解决方案

您可以执行以下任一操作

  1. 您可以在活动或服务中注册,而不是在清单中注册。参见this answer.
  2. 一个人欺骗了奥利奥牛轧糖设备清单中的广播接收器。参见this repo

更新

请参见documentation

ACTION_MY_PACKAGE_REPLACED

  

广播操作:已安装新版本的应用程序   在一个现有的。这仅发送到   更换。它不包含任何其他数据;接收它,只是   为此操作使用意图过滤器。

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

答案 1 :(得分:0)

ACTION_MY_PACKAGE_REPLACED的文档说:

  

已在现有应用程序上安装了新版本的应用程序   一。这仅发送到已替换的应用程序。确实   不包含任何其他数据;接收它,只是用一个意图   对此操作进行过滤。

这意味着旧应用应该接收广播,而不是新应用。因此,至少您不能使用它来启动应用程序。

如果您想通过代码手动更新apk,请选中here

答案 2 :(得分:0)

download sdk if not exists in your pc SDK Path \ platform tools文件夹,我的意思是“ C:\ AndroidStudio_SDK \ platform-tools”。

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

  • 之后,执行adb shell广播-android.intent.action.MY_PACKAGE_REPLACED

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


命令提示符->广播外壳-android.intent.action.MY_PACKAGE_REPLACED -n com.example.myappname / .UpdateReceiver

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

相关问题