3

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...");
    }
}
Happy
  • 1,031
  • 10
  • 26
  • " suddenly i could not receive event" - are you implying you had working code before? – Pawel Nov 17 '21 at 12:17
  • @Pawel Sorry, Previously i was using this event "android.intent.action.PACKAGE_REPLACED" where now it fails. i changed to MY_PACKAGE_REPLACED. but still failing to get the event. – Happy Nov 17 '21 at 12:20
  • I don't understand your sentence. Have this receiver ever worked or not? – Pawel Nov 17 '21 at 12:23
  • yes. Action_PACKAGE_REPLACED was working but not now. @Pawel – Happy Nov 17 '21 at 12:25

2 Answers2

3

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")
     }
}
Happy
  • 1,031
  • 10
  • 26
1

In your receiver, you should change this:

Intent.ACTION_PACKAGE_REPLACED.equals(action)

for this:

Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)
jmart
  • 2,769
  • 21
  • 36