ACTION_MY_PACKAGE_REPLACED not received

后端 未结 9 571
长发绾君心
长发绾君心 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:14

    Getting information from all the users I could solve my situation this way. All of them were right, with little points to notice:

    In manifest:

        <receiver android:name=".MyEventReceiver" >
            <intent-filter android:priority="1000" >
                <!--other actions I need-->
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
    

    And code:

    public class MyEventReceiver extends BroadcastReceiver
    {     
        @Override public void onReceive(Context context, Intent intent)
        {  
           if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) 
           {   if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName()))
               {  //Restart services.
               }
           }
        }      
    }
    

    In my Android release (2.3 Gingerbread) I was not able to use MY_PACKAGE_REPLACED but we solved using PACKAGE_REPLACED (will advise of any app been replaced) but asking if it is ours with:

     if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName()))
     {
     }
    

    Thanks to all

    0 讨论(0)
  • 2020-12-08 01:15

    I just want to add my own two pence worth here, because I had problems getting this to work and debugging it .. I am using a Lollipop device:

    This is the code I used:

        <receiver android:name=".services.OnUpdateReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            </intent-filter>
        </receiver>    
    

    To debug it, you need to make sure you have installed the update on your phone, just via Eclipse debug is fine, and opened the app at least one time, then you can simply edit your debug configuration:

    eclipse > run > debug configurations > launch action (do nothing) > then F11 like normal
    

    I confirmed it worked by writing a small file to the SD card

    0 讨论(0)
  • 2020-12-08 01:22

    I ran into this problem when I was building and installing my app from Android Studio.

    • When doing normal build & run (without debugging), I fixed the problem by turning off Instant Run.
    • When doing a build & run with debugging, I couldn't find a way to fix the problem.
    0 讨论(0)
  • 2020-12-08 01:23

    The accepted answer doesn't work any more with Android Studio 1.0+ because of manifest merge issues, as seen here. Totally based on android developer's answer, I fixed the issue with the following implementation:

    AndroidManifest.xml:

    <receiver android:name=".UpdateReceiver$LegacyUpdateReceiver" android:enabled="@bool/shouldUseActionPackageReplaced" >
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>
    <receiver android:name=".UpdateReceiver" android:enabled="@bool/shouldUseActionMyPackageReplaced" >
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
    </receiver>
    

    /res/values/resources.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="shouldUseActionPackageReplaced">true</bool>
        <bool name="shouldUseActionMyPackageReplaced">false</bool>
    </resources>
    

    /res/values-v12/resources.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="shouldUseActionPackageReplaced">false</bool>
        <bool name="shouldUseActionMyPackageReplaced">true</bool>
    </resources>
    

    UpdateReceiver.java:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class UpdateReceiver extends BroadcastReceiver
    {
        public static class LegacyUpdateReceiver extends BroadcastReceiver
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                if (intent != null && intent.getData() != null && context.getPackageName().equals(intent.getData().getSchemeSpecificPart()))
                {
                    onUpdate(context);
                }
            }
        }
    
        @Override
        public void onReceive(Context context, Intent intent)
        {
            onUpdate(context);
        }
    
        public static void onUpdate(Context context)
        {
            Log.d("LOG", "Current app updated");
        }
    }
    
    0 讨论(0)
  • 2020-12-08 01:27

    Are you trying it on a API>=12 device/emulator? This broadcast will not be sent on prior cases as it is API 12. If you need your app to receive this for Pre-ICS and the old honey comb devices,

    try:

    if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
      if (!intent.getData().getSchemeSpecificPart()
           .equals(context.getPackageName())) {
        return;
      }
    }
    
    0 讨论(0)
  • 2020-12-08 01:27

    Simple Manifest working in all version :

        <receiver
            android:name=".Receiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <data android:scheme="package"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            </intent-filter>
        </receiver>
    
    0 讨论(0)
提交回复
热议问题