ACTION_MY_PACKAGE_REPLACED not received

后端 未结 9 714
长发绾君心
长发绾君心 2020-12-08 00:59

I am using ACTION_MY_PACKAGE_REPLACED to receive when my app is updated or resinstalled. My problem is that the event is never triggered (I tried Eclipse and real device). T

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 01:38

    for some reason, a google developer (named "Dianne Hackborn") thinks it is possible to register to the PACKAGE_REPLACED intent of the current app alone (read archived version here, original link here).

    however, i can't find any way of doing it correctly, so i've made a compromise: it will use the newest API when available.

    Sadly, I can't find out why it can't be debugged, but it does work (you can write to the log if you wish).

    here's the code:

    manifest:

        
            
                
                
            
        
        
            
                
            
        
    

    res/values/version_checks.xml

    
    
    
        false
        true
    
    
    

    res/values-v12/version_checks.xml

    
    
    
        true
        false
    
    
    

    OnUpgradeBroadcastReceiver.java

    public class OnUpgradeBroadcastReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(final Context context, final Intent intent) {
            if (VERSION.SDK_INT <= VERSION_CODES.HONEYCOMB
                    && !context.getPackageName().equals(intent.getData().getSchemeSpecificPart())) {
                android.util.Log.d("AppLog", "other apps were upgraded");
                return;
            }
            android.util.Log.d("AppLog", "current app was upgraded");
    

    EDIT: In today's Android versions, when you should set minSdk to be at least 14, you don't need this, and indeed you should just use MY_PACKAGE_REPLACED and that's it. No need for the booleans etc...

提交回复
热议问题