androidandroid-intentandroid-sdk-toolstarget-sdk

MY_PACKAGE_REPLACED stops working - How to find app upgrade android


I wanted to find the upgrade of my app. So i have been using this code to find it PACKAGE_REPLACED, but suddenly i could not receive event for package replacing of my app.

And i changed to MY_PACKAGE_REPLACED. still same issue.

Analysed some stack over flow questions. no luck. tried all of the answer.

My target sdk version is 30:

Manifest Code:

<receiver android:name=".Receiver" android:enabled="true" android:debuggable="true" android:exported="true"
    tools:ignore="HardcodedDebugMode">
  <intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACED" />

  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  </intent-filter>
</receiver>

Receiver code

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Log.d(TAG, "action = " + action);

    if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
        Log.d(TAG, "BOOT COMPLETED, Ping start...");
    } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
        Log.d(TAG, "PACKAGE REPLACED, upgrade ping...");
    } else {
        //default action is network changed
        Log.d(TAG, "network status changed...");
    }
}

Solution

  • Answering myself after analysing more stackoverlfow questions/answers and comments.

    Previously, i was using PACKAGE_REPLACED event to receive the update and i have added package check as we receive this event for all package replaces.

    But suddenly or i noticed now, app stops receiving the PACKAGE_REPLACED event.

    And answers from stackoverflow says, we should use MY_PACKAGE_REPLACED . which will not give any additional data.

    Also, i have replaced it to MY_PACKAGE_REPLACED and tried by normal apk running from Android Studio. This where i have made a mistake.

    Looks, we should run using the adb command, then only it triggers the package replace event

    ./adb install /Users/vijay/desktop/android/myapp.apk

    The complete code:

    manifest:

    <receiver android:name=“.MPackageReplacedReceiver">
       <intent-filter>
       <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
       </intent-filter>
    </receiver>
    

    Receiver code

    class MyPackageReplacedReceiver : BroadcastReceiver() {
         override fun onReceive(context: Context, intent: Intent) {
           Log.d(TAG, "package replaced event")
         }
    }