Android利用系统广播---监听应用程序安装和卸载

来源:互联网 发布:淘宝魅力惠 编辑:程序博客网 时间:2024/04/25 18:56

   在android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()获得所安装的包名。当卸载程序时系统发android.intent.action.PACKAGE_REMOVED广播。同样intent.getDataString()获得所卸载的包名。 


第一、 新建监听类:BootReceiver继承BroadcastReceiver 
Java代码  收藏代码
  1. public class BootReceiver extends BroadcastReceiver {      
  2.      
  3.     @Override     
  4.     public void onReceive(Context context, Intent intent) {      
  5.         //接收广播:系统启动完成后运行程序      
  6.         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {      
  7.              Intent newIntent = new Intent(context, WatchInstall.class);      
  8.              newIntent.setAction("android.intent.action.MAIN");         
  9.              newIntent.addCategory("android.intent.category.LAUNCHER");       
  10.              newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         
  11.              context.startActivity(newIntent);      
  12.         }      
  13.         //接收广播:设备上新安装了一个应用程序包后自动启动新安装应用程序。      
  14.         if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {      
  15.             String packageName = intent.getDataString().substring(8);      
  16.             System.out.println("---------------" + packageName);      
  17.             Intent newIntent = new Intent();      
  18.             newIntent.setClassName(packageName,packageName+ .MainActivity");      
  19.             newIntent.setAction("android.intent.action.MAIN");               
  20.             newIntent.addCategory("android.intent.category.LAUNCHER");               
  21.             newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
  22.             context.startActivity(newIntent);      
  23.         }      
  24.         //接收广播:设备上删除了一个应用程序包。      
  25.         if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {      
  26.             System.out.println("********************************");      
  27.             DatabaseHelper dbhelper = new DatabaseHelper();      
  28.             dbhelper.executeSql("delete from users");      
  29.         }      
  30.     }     


第二、 修改AndroidManifest.xml配置文件 
Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>     
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"     
  3.      package="org.me.watchinstall">     
  4.     <application>     
  5.         <receiver android:name=".BootReceiver"     
  6.                   android:label="@string/app_name">     
  7.             <intent-filter>     
  8.                 <action android:name="android.intent.action.BOOT_COMPLETED"/>     
  9.                 <category android:name="android.intent.category.LAUNCHER" />     
  10.             </intent-filter>     
  11.             <intent-filter>     
  12.              <action android:name="android.intent.action.PACKAGE_ADDED" />     
  13.              <action android:name="android.intent.action.PACKAGE_REMOVED" />     
  14.               <data android:scheme="package" />     
  15. <!--[color=red] 注意!! 这句必须要加,否则接收不到BroadCast  [/color] -->   
  16.             </intent-filter>     
  17.         </receiver>     
  18.         <activity android:name=".WatchInstall" android:label="WatchInstall">     
  19.             <intent-filter>     
  20.                 <action android:name="android.intent.action.MAIN"/>     
  21.                 <category android:name="android.intent.category.LAUNCHER"/>     
  22.             </intent-filter>     
  23.         </activity>     
  24.     </application>     
  25.     <uses-permission android:name="android.permission.INTERNET" />     
  26.     <uses-permission android:name="android.permission.RESTART_PACKAGES"/>     
  27.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>     
  28. </manifest>