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

来源:互联网 发布:透视软件 编辑:程序博客网 时间:2024/04/26 19:04

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

【科学上网软件点击下载(能上youtube、facebook,享受google服务)】

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



第二、 修改AndroidManifest.xml配置文件

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>  
原创粉丝点击