我的应用不听ACTION_MY_PACKAGE_REPLACED

时间:2018-03-01 15:29:31

标签: android broadcastreceiver alarmmanager

我注意到当App更新时,Intent全部消失,所以我需要这个应用程序来监听App更新并重新发送意图,就像它与BOOT_COMPLETE一样

的AndroidManifest.xml

       <receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
        </intent-filter>
    </receiver>

AlarmReceiver.java

    public class AlarmReceiver  extends BroadcastReceiver{
    private AlarmManager alarmManager;
    private AlarmDB pAlarms;
    @Override
    public void onReceive(Context context, Intent intent) {
        int reqCode = intent.getIntExtra("requestCode", 0);
        System.out.println("Received code:" + reqCode);
        alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        pAlarms= new AlarmDB();
        pLoadAlarms(context);
        System.out.println("Received Signal:" + intent.getAction());
        if(Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())){
            System.out.println("Replaceing Packing");
            for(int i=0;i<pAlarms.getSize();i++){
                if(pAlarms.get(i).enabled){
                    setAlarm(context,pAlarms.get(i));}
            }
        }
    }
...
}

我用adb shell测试了ACTION

C:\Users\-----\AppData\Local\Android\sdk\platform-tools>adb shell am broadcast -a ACTION_MY_PACKAGE_REPLACED
Broadcasting: Intent { act=ACTION_MY_PACKAGE_REPLACED }
Broadcast completed: result=0

但是在Logcat上,它不会在代码中打印任何内容,因此我认为甚至没有收到任何信号。

但是,如果我在gradle更改versionCode并通过android studio更新应用程序,则所有广播信号都会消失,并且不会调用/接收MY_PACKAGE_REPLACED。

0 个答案:

没有答案