Install and uninstall Android listeners

Source: Internet
Author: User

The installation and uninstallation events of Android applications are monitored and broadcast globally by the system, and support for Android 1.5 and later versions

Therefore, if you want to monitor and obtain the installation and uninstall events of an application, you only need to customize a broadcastreceiver to listen to and process system broadcasts.

Broadcastreceiver is a global broadcast listener class. Its main method is onreceive (). The custom broadcast class inherits from it and implements its own onreceive () processing logic.

Before using broadcastreceiver, You need to register the listener (XML and code). You need to cancel the listener when not in use. The life cycle is generally the life cycle of the application.

1. Custom Broadcast

Custom broadcast myinstalledreceiver inherits from broadcastreceiver and implements its onreceive () method. The Code is as follows:

Public class myinstalledreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {If (intent. getaction (). equals ("android. intent. action. package_added ") {// installstring packagename = intent. getdatastring (); log. I ("Homer", "installed:" + packagename);} If (intent. getaction (). equals ("android. intent. action. package_removed ") {// uninstallstring packagename = intent. getdatastring (); log. I ("Homer", "uninstalled:" + packagename );}}}

2. register the listener

1) XML

Under the application node of the androidmanifest. xml configuration file, add a custom registration listener myinstalledreceiver

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.homer.installed"    android:versionCode="1"    android:versionName="1.0" >    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver android:name=".MyInstalledReceiver" >            <intent-filter>                <action android:name="android.intent.action.PACKAGE_ADDED" />                <action android:name="android.intent.action.PACKAGE_REMOVED" />                <data android:scheme="package" />            </intent-filter>        </receiver>    </application>    <uses-sdk android:minSdkVersion="3" /></manifest>

The registration listener added in androidmanifest. XML has a life cycle of the entire application by default.

2) code method

Generally, the listener is registered in the onstart () method of the activity, and the listener is canceled in the ondestroy () method (you can also log out in the onstop () method, and its lifecycle ends when it is canceled)

@Overridepublic void onStart(){super.onStart();installedReceiver = new MyInstalledReceiver();IntentFilter filter = new IntentFilter();filter.addAction("android.intent.action.PACKAGE_ADDED");filter.addAction("android.intent.action.PACKAGE_REMOVED");filter.addDataScheme("package");this.registerReceiver(installedReceiver, filter);}@Overridepublic void onDestroy(){if(installedReceiver != null) {this.unregisterReceiver(installedReceiver);}super.onDestroy();}

The above two registration methods are XML and code. You can select one of them for use;

If two methods are used at the same time, both methods are valid, that is, two installation or uninstallation operations are counted (repeated statistics)

3. Result Test

Source code download

Reference recommendations:

Installing ing package install and uninstall events

Android broadcastreceiver (recommended)

Android manifest. XML structure

Android intent action Overview

Intent (Google)

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.