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

来源:互联网 发布:网络电话源码 编辑:程序博客网 时间:2024/04/29 20:06

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


第一、 新建监听类:BootReceiver继承BroadcastReceiver
Java代码 收藏代码
  1. publicclass BootReceiverextends BroadcastReceiver {
  2. @Override
  3. publicvoid 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配置文件
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> 
0 0
原创粉丝点击