First, you need to obtain the application installation status, in the form of Broadcast
The following are application-related Broadcast actions:
ACTION_PACKAGE_ADDED a new application package has been installed on the device, and the data includes the package name (this broadcast cannot be received by the latest package Program)
ACTION_PACKAGE_REPLACED install a new version of the application to the device and replace the existing version.
ACTION_PACKAGE_CHANGED an existing application package, including the package name
ACTION_PACKAGE_REMOVED an existing application package has been removed from the device, including the package name (this broadcast cannot be received by the package program being installed)
The ACTION_PACKAGE_RESTARTED user restarts a package, and all processes in the package will be killed. All running time States associated with the package should be removed, including the package name (the package re-start program cannot receive this broadcast)
The ACTION_PACKAGE_DATA_CLEARED user knows the data of a package, including the package name (the package clearing program cannot receive this broadcast)
Code Implementation
Define broadcast in AndroidManifest. xml
Copy codeThe Code is as follows:
<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>
</Cycler>
Here we use
ACTION_PACKAGE_ADDED a new application package has been installed on the device, and the data includes the package name (this broadcast cannot be received by the latest package Program)
ACTION_PACKAGE_REPLACED install a new version of the application to the device and replace the existing version.
ACTION_PACKAGE_REMOVED an existing application package has been removed from the device, including the package name (this broadcast cannot be received by the package program being installed)
Let's look at AppInstallReceiver.
Copy codeThe Code is as follows:
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, "installed successfully" + packageName, Toast. LENGTH_LONG). show ();
}
If (intent. getAction (). equals (Intent. ACTION_PACKAGE_REMOVED )){
String packageName = intent. getData (). getSchemeSpecificPart ();
Toast. makeText (context, "uninstalled successfully" + packageName, Toast. LENGTH_LONG). show ();
}
If (intent. getAction (). equals (Intent. ACTION_PACKAGE_REPLACED )){
String packageName = intent. getData (). getSchemeSpecificPart ();
Toast. makeText (context, "replaced successfully" + packageName, Toast. LENGTH_LONG). show ();
}
}
}
The code implementation is relatively simple. Based on the received Action, determine whether the application is installed, uninstalled, or replaced with another version.