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

来源:互联网 发布:ecmascript javascript 编辑:程序博客网 时间:2024/04/20 23:27

第一、 新建监听类:BootReceiver继承BroadcastReceiver


public class BootReceiver extends BroadcastReceiver {  

 @Override  

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

第二、 修改AndroidManifest.xml配置文件
  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. <!-- 注意!! 这句必须要加,否则接收不到BroadCast -->  
  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> 



原创粉丝点击