Android监听应用程序安装和卸载

来源:互联网 发布:codemirror.js 编辑:程序博客网 时间:2024/06/05 11:30

    在Android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()获得所安装的包名。当卸载程序时系统发android.intent.action.PACKAGE_REMOVED广播。同样intent.getDataString()获得所卸载的包名。应用程序无法监听自己的安装与卸载,但覆盖安装可以监听到自己的android.intent.action.PACKAGE_REMOVED广播。

第一、 新建监听类:BootReceiver继承BroadcastReceiver 
[java] view plaincopy
  1. package com.rongfzh.yc;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.   
  7. public class BootReceiver extends BroadcastReceiver{  
  8.         
  9.     @Override    
  10.     public void onReceive(Context context, Intent intent){  
  11.         //接收安装广播   
  12.         if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {     
  13.             String packageName = intent.getDataString();     
  14.             System.out.println("安装了:" +packageName + "包名的程序");       
  15.         }     
  16.         //接收卸载广播    
  17.         if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {     
  18.             String packageName = intent.getDataString();     
  19.             System.out.println("卸载了:"  + packageName + "包名的程序");  
  20.    
  21.         }  
  22.     }  
  23. }    

第二、 修改AndroidManifest.xml配置文件,添加广播介绍,添加监听的权限
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.rongfzh.yc"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="7" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".PakDetectActivity"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.         <receiver android:name=".BootReceiver"    
  22.                   android:label="@string/app_name">     
  23.             <intent-filter>    
  24.              <action android:name="android.intent.action.PACKAGE_ADDED" />    
  25.              <action android:name="android.intent.action.PACKAGE_REMOVED" />    
  26.               <data android:scheme="package" />    
  27.             </intent-filter>    
  28.         </receiver>    
  29.     </application>  
  30.     <uses-permission android:name="android.permission.INTERNET" />    
  31.     <uses-permission android:name="android.permission.RESTART_PACKAGES"/>    
  32.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  
  33.   
  34. </manifest>  

3、运行程序,卸载一个程序ApiDemos程序打印日志如下
 System.out(1513): 卸载了:package:com.example.android.apis包名的程序
4、安装腾讯微博,打印日志如下:
System.out(1513): 安装了:package:com.tencent.WBlog包名的程序

程序监听成功。

程序例子代码:
http://download.csdn.net/detail/totogo2010/4390605

原文地址:点击打开链接