Android uses broadcast listening devices to install and uninstall applications

Source: Internet
Author: User

MainActivity is as follows:

Package CN. testappaddandremove; import android. OS. bundle; import android. app. activity; import android. content. intentfilter;/*** demo Description: * use a broadcast listening device to install and uninstall an application ** reference: * http://blog.csdn.net/wangjinyu501/article/details/9664315 * Thank you very much */public class mainactivity extends activity {private appbroadcastreceiver mappbroadcastreceiver; @ overrideprotected void oncreate (bundle savedinstancestat E) {super. oncreate (savedinstancestate); setcontentview (R. layout. main) ;}@ override protected void onstart () {super. onstart (); // Method 1: Set intentfilter in the Code // mappbroadcastreceiver = new appbroadcastreceiver (); // intentfilter = new intentfilter (); // intentfilter. addaction ("android. intent. action. package_added "); // intentfilter. addaction ("android. intent. action. package_removed "); // intentfilter. A Dddatascheme ("package"); // This. registerreceiver (mappbroadcastreceiver, intentfilter); // Method 2: In manifest. set intentfilter in XML // test method 2. Better Results mappbroadcastreceiver = new appbroadcastreceiver (); intentfilter = new intentfilter (); this. registerreceiver (mappbroadcastreceiver, intentfilter) ;}@ overrideprotected void ondestroy () {If (mappbroadcastreceiver! = NULL) {This. unregisterreceiver (mappbroadcastreceiver);} super. ondestroy ();}}

AppBroadcastReceiver is as follows:

Package cn. testappaddandremove; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; public class AppBroadcastReceiver extends BroadcastReceiver {private final String ADD_APP = "android. intent. action. PACKAGE_ADDED "; private final String REMOVE_APP =" android. intent. action. PACKAGE_REMOVED "; @ Overridepublic void onReceive (Context context, Intent intent) {String action = intent. getAction (); if (ADD_APP.equals (action) {String packageName = intent. getDataString (); System. out. println ("installed:" + packageName);} if (REMOVE_APP.equals (action) {String packageName = intent. getDataString (); System. out. println ("uninstalled:" + packageName );}}}

Manifest. xml is as follows:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.testappaddandremove"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="8" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="cn.testappaddandremove.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="cn.testappaddandremove.AppBroadcastReceiver">            <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></manifest>

Main. xml is as follows:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "listening for application installation and uninstallation" android: layout_centerInParent = "true"/> </RelativeLayout>

 

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.