Android SDK monitors apk installation, replaces and uninstalls broadcast implementation code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.