监听应用程序包安装删除重装

来源:互联网 发布:php单例模式是什么 编辑:程序博客网 时间:2024/06/15 02:15
  1. public class GetBroadcastextends BroadcastReceiver {
  2. private static GetBroadcast mReceiver =new GetBroadcast();
  3. private static IntentFilter mIntentFilter;
  4. public staticvoid registerReceiver(Context context) {
  5. mIntentFilter = new IntentFilter();
  6. mIntentFilter.addDataScheme("package");
  7. mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
  8. mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  9. mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
  10. context.registerReceiver(mReceiver, mIntentFilter);
  11. }
  12. public staticvoid unregisterReceiver(Context context) {
  13. context.unregisterReceiver(mReceiver);
  14. }
  15. @Override
  16. public void onReceive(Context context, Intent intent) {
  17. String action = intent.getAction();
  18. if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
  19. Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
  20. } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
  21. Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
  22. }
  23. /*
  24. * else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){
  25. * Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); }
  26. */
  27. else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
  28. Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
  29. }
  30. /*
  31. * else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){
  32. * Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); }
  33. */
  34. /*
  35. * else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){
  36. * Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); }
  37. */
  38. }
  39. }
public class GetBroadcast extends BroadcastReceiver {private static GetBroadcast mReceiver = new GetBroadcast();private static IntentFilter mIntentFilter;public static void registerReceiver(Context context) {mIntentFilter = new IntentFilter();mIntentFilter.addDataScheme("package");mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);context.registerReceiver(mReceiver, mIntentFilter);}public static void unregisterReceiver(Context context) {context.unregisterReceiver(mReceiver);}@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();}/** else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){* Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); }*/else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();}/** else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){* Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); }*//** else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){* Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); }*/}}

 

 

public class BootReceiver extends BroadcastReceiver{
  
    @Override 
    public void onReceive(Context context, Intent intent){
        //接收安装广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {  
            String packageName = intent.getDataString();  
            System.out.println("安装了:" +packageName + "包名的程序");    
        }  
        //接收卸载广播 
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {  
         String packageName = intent.getDataString();  
            System.out.println("卸载了:"  + packageName + "包名的程序");
 
        }
    }

 

 

 

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

2011-10-28 00:49:30|  分类: Android|字号 订阅

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

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

第二、 修改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>

 

 

 

 

 


 

原创粉丝点击