Android listens for the implementation code for apk to install replace and uninstall broadcasts

  • 2020-05-17 06:29:49
  • OfStack

The first is to get the installation status of the application, via broadcast
Here are the application-specific Broadcast Action
ACTION_PACKAGE_ADDED 1 new application package has been installed on the device, and the data includes the package name (the newly installed package cannot receive this broadcast)
A new version of ACTION_PACKAGE_REPLACED application is installed on the device, replacing the existing version
ACTION_PACKAGE_CHANGED 1 existing application package has been changed, including the package name
ACTION_PACKAGE_REMOVED 1 existing application package has been removed from the device, including the package name (the package being installed cannot receive this broadcast)
If the ACTION_PACKAGE_RESTARTED user restarts a package, all processes of the package will be killed, and all runtime states associated with the package will be removed, including the package name (restarting the package cannot receive this broadcast).
The ACTION_PACKAGE_DATA_CLEARED user already knows the data of a package, including the package name (the clear package cannot receive this broadcast)

Code implementation
Define broadcasting in AndroidManifest.xml

<receiver android:name=".AppInstallReceiver"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" /> 
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>

I'm going to use theta here
ACTION_PACKAGE_ADDED 1 new application package has been installed on the device, and the data includes the package name (the newly installed package cannot receive this broadcast)
A new version of the ACTION_PACKAGE_REPLACED application is installed on the device, replacing the existing version
ACTION_PACKAGE_REMOVED 1 existing application package has been removed from the device, including the package name (the package being installed cannot receive this broadcast)
Then look at AppInstallReceiver

public class AppInstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        PackageManager manager = context.getPackageManager();
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, " Successful installation "+packageName, Toast.LENGTH_LONG).show();
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, " Uninstall the success "+packageName, Toast.LENGTH_LONG).show();
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, " Replace the success "+packageName, Toast.LENGTH_LONG).show();
        }

    }
}

The code implementation is simple, based on the Action received to determine whether the application is installed or uninstalled or replaced with another version

Related articles: