Android 监控程序装配和删除的实现

Android 监控程序安装和删除的实现通过阅读Android SDK里关于intent.action这部分里面的描述,我们可以找到

Android 监控程序安装和删除的实现

通过阅读Android SDK里关于intent.action这部分里面的描述,我们可以找到一些与package相关的系统广播

  1. android.intent.action.PACKAGE_ADDED???????android.intent.action.PACKAGE_CHANGED???????
  2. android.intent.action.PACKAGE_DATA_CLEARED???????android.intent.action.PACKAGE_INSTALL?????
  3. android.intent.action.PACKAGE_REMOVED?????android.intent.action.PACKAGE_REPLACED???????
  4. android.intent.action.PACKAGE_RESTARTED??

其中

ACTION_PACKAGE_ADDED

在SDK里的描述是 ?

Broadcast Action: A new application package has been installed on the device.

ACTION_PACKAGE_REMOVED

在SDK里的描述是

Broadcast Action: An existing application package has been removed from the device.

ACTION_PACKAGE_REPLACED

在SDK里的描述是

Broadcast Action: A new version of an application package has been installed, replacing an existing version that was previously installed.

通过这三个广播消息 我们已经可以监控到Android 应用程序的安装和删除

详细的实现代码如下:

  1. package?zy.Broadcast;???import?android.content.BroadcastReceiver;???
  2. import?android.content.Context;???import?android.content.Intent;???
  3. import?android.widget.Toast;???public?class?getBroadcast?extends?BroadcastReceiver?{???
  4. ????????@Override???????????public?void?onReceive(Context?context,?Intent?intent)?{???
  5. ????????????????????????????????????if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){???
  6. ????????????????????Toast.makeText(context,?"有应用被添加",?Toast.LENGTH_LONG).show();???????????????}???
  7. ????????????????else??if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){???????????????????????Toast.makeText(context,?"有应用被删除",?Toast.LENGTH_LONG).show();???
  8. ????????????}????????????????/*???else??if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){??
  9. ????????????????????Toast.makeText(context,?"有应用被改变",?Toast.LENGTH_LONG).show();??????????????}*/???
  10. ????????????????else??if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){???????????????????????Toast.makeText(context,?"有应用被替换",?Toast.LENGTH_LONG).show();???
  11. ????????????}??????????????????/*?else??if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){??
  12. ????????????????????Toast.makeText(context,?"有应用被重启",?Toast.LENGTH_LONG).show();??????????????}*/???
  13. ??????????????/*??else??if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){??????????????????????Toast.makeText(context,?"有应用被安装",?Toast.LENGTH_LONG).show();??
  14. ????????????}*/??????????????????
  15. ????????}?????????????
  16. }???

然后在AndroidManifest.xml中声明这几个Action的<intent-filter>即可在系统里捕获这些广播消息

具体的源代码如下

  1. <receiver?android:name="getBroadcast"?android:enabled="true"?>????????????<intent-filter>???
  2. ?????????????<action?android:name="android.intent.action.PACKAGE_ADDED"></action>????????????????<!--?<action?android:name="android.intent.action.PACKAGE_CHANGED"></action>-->???
  3. ?????????????<action?android:name="android.intent.action.PACKAGE_REMOVED"></action>????????????????<action?android:name="android.intent.action.PACKAGE_REPLACED"></action>???
  4. ?????????????<!--?<action?android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->??????????????<!--????<action?android:name="android.intent.action.PACKAGE_INSTALL"></action>-->???
  5. ???????????????<data?android:scheme="package"></data>?????????????????</intent-filter>???
  6. </receiver>??

另: intent.getDataString()可以得到安装的是哪个apk,如:

????? package:com.android.myapp

?

?

源:http://maxuefeng.blog.51cto.com/1876326/528708