Android Listening APK installation replacement uninstall broadcast implementation code

Source: Internet
Author: User

The first is to obtain the installation state of the application by broadcasting the form
The following is an application-related broadcast Action
action_package_added A new application package is already 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 to 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_restarted users to restart a package, all processes of the package will be killed, all the running time status associated with it should be removed, including the package name (restart package cannot receive this broadcast)
action_package_data_cleared users already know a packet's data, including the package name (the removal package does not receive this broadcast)

Code Implementation
define a broadcast in Androidmanifest.xml

Copy Code code 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>


</receiver>


Here is a selection of
action_package_added A new application package is already 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 to 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)
And look at Appinstallreceiver.

Copy Code code 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, "Uninstall Success" +packagename, Toast.length_long). Show ();


        }


if (intent.getaction (). Equals (intent.action_package_replaced)) {


String PackageName = Intent.getdata (). Getschemespecificpart ();


Toast.maketext (Context, "replace success" +packagename, Toast.length_long). Show ();


        }





    }


}


Code implementation is relatively simple, based on the received action to determine whether the application is an loading or unloading or replaced by 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.