First, you need to obtain the installation status of the application and use the broadcast form.The following is the Broadcast Action related to the application
ACTION_PACKAGE_ADDED A new application package has been installed on the device, and the data includes the package name (the latest installed package program cannot receive this broadcast)
ACTION_PACKAGE_REPLACED A new version of the application is installed on the device, replacing the previously existing version
ACTION_PACKAGE_CHANGED An existing application package has been changed, including the package name
ACTION_PACKAGE_REMOVED An existing application package has been removed from the device, including the package name (the package program being installed cannot receive this broadcast)
ACTION_PACKAGE_DATA_CLEARED The user has already known the data of a packet, including the packet name (the clearing package program cannot receive this broadcast)
Code implementation
Define broadcast in
<receiver android:name=".AppInstallReceiver"
android:label="@string/app_name">
<intent-filter>
<action android:name=".PACKAGE_ADDED" />
<action android:name=".PACKAGE_REPLACED" />
<action android:name=".PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
The one you choose here is
ACTION_PACKAGE_ADDED A new application package has been installed on the device, and the data includes the package name (the latest installed package program cannot receive this broadcast)
ACTION_PACKAGE_REPLACED A new version of the application is installed on the device, replacing the previously existing version
ACTION_PACKAGE_REMOVED An existing application package has been removed from the device, including the package name (the package program being installed cannot receive this broadcast)
Look at AppInstallReceiver
public class AppInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PackageManager manager = ();
if (().equals(Intent.ACTION_PACKAGE_ADDED)) {
String packageName = ().getSchemeSpecificPart();
(context, "Installation Successfully" + packageName, Toast.LENGTH_LONG).show();
}
if (().equals(Intent.ACTION_PACKAGE_REMOVED)) {
String packageName = ().getSchemeSpecificPart();
(context, "Uninstalled successfully" + packageName, Toast.LENGTH_LONG).show();
}
if (().equals(Intent.ACTION_PACKAGE_REPLACED)) {
String packageName = ().getSchemeSpecificPart();
(context, "Replace successfully" + packageName, Toast.LENGTH_LONG).show();
}
}
}
The code implementation is relatively simple. Based on the received Action, we can determine whether the application is installed, uninstalled or replaced with another version.