Problem recurrence
A recent project encountered a problem: when the software is overwritten and installed, the system will send out the following broadcasts in sequence:
So how do we know that the ADDED and REMOVED broadcasts are actions sent by REPLACED?
solution
intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
It can be used to determine whether installation and uninstallation come from notifications covering installations. Attached is the code:
public class AppReceiver extends BroadcastReceiver {
private AppReceiverListener mAppReceiverListener;
public interface AppReceiverListener {
void added(String pageName);
void remove(String pageName);
void replaced(String pageName);
}
public void setAppReceiverListener(AppReceiverListener appReceiverListener) {
mAppReceiverListener = appReceiverListener;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action) && mAppReceiverListener != null
&& !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
String pageName = intent.getData().getSchemeSpecificPart();
mAppReceiverListener.added(pageName);
Log.e("AppReceiver", ": ACTION_PACKAGE_ADDED 软件已安装");
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && mAppReceiverListener != null
&& !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
String pageName = intent.getData().getSchemeSpecificPart();
mAppReceiverListener.remove(pageName);
Log.e("AppReceiver", ": ACTION_PACKAGE_REMOVED 软件已卸载");
} else if (Intent.ACTION_PACKAGE_REPLACED.equals(action) && mAppReceiverListener != null) {
String pageName = intent.getData().getSchemeSpecificPart();
mAppReceiverListener.replaced(pageName);
Log.e("AppReceiver", ": ACTION_PACKAGE_REPLACED 软件已覆盖");
}
}
}
The idea comes from:
Package replacement process in android