Android app update broadcast

13.3k views Asked by At

So, im working on something that requires me to know when another application on the device is being updated. So my question is quite simple, does lets say YouTube or Spotify send a broadcast when the application is updating, and if so, what do i need to catch with my broadcastReceiver.

3

There are 3 answers

2
cristi.onisimi On BEST ANSWER

Your intent filter should be like:

<intent-filter>
   <action android:name="android.intent.action.PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>

And onReceive method from your BroadcastReceiver should be:

@Override
public void onReceive(Context context, Intent intent) {
    Uri data = intent.getData();
    if (data.toString().equals("package:" + "com.target.package") {
        // action to do
    }
}
3
Kartheek On

ACTION_PACKAGE_REPLACED is action triggered when an application is being updated According to docs :

A new version of an application package has been installed, replacing an existing version that was previously installed. The data contains the name of the package.

intent.getPackage() returns the package/application this intent was specifically addressed to but it was sent to any interested receiver, therefore, there isn't such package.

Use intent.getData() which returns the updated package as an Uri

Add the below intent filter while registering a broadcast receiver in your Manifest.xml

<intent-filter>
   <action android:name="android.intent.action.PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>
3
Ashish John On

According to android docs: Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.

<intent-filter>
   <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
   <data android:scheme="package" />
</intent-filter>

And onReceive method from your BroadcastReceiver should be:

@Override
public void onReceive(Context context, Intent intent) {
    // action to do

}