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

  
  1. android.intent.action.PACKAGE_ADDED
  2. android.intent.action.PACKAGE_CHANGED
  3. android.intent.action.PACKAGE_DATA_CLEARED
  4. android.intent.action.PACKAGE_INSTALL
  5. android.intent.action.PACKAGE_REMOVED
  6. android.intent.action.PACKAGE_REPLACED
  7. 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. packagezy.Broadcast;
  2. importandroid.content.BroadcastReceiver;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. importandroid.widget.Toast;
  6. publicclassgetBroadcastextendsBroadcastReceiver{
  7. @Override
  8. publicvoidonReceive(Contextcontext,Intentintent){
  9. if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
  10. Toast.makeText(context,"有应用被添加",Toast.LENGTH_LONG).show();
  11. }
  12. elseif(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
  13. Toast.makeText(context,"有应用被删除",Toast.LENGTH_LONG).show();
  14. }
  15. /*elseif(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){
  16. Toast.makeText(context,"有应用被改变",Toast.LENGTH_LONG).show();
  17. }*/
  18. elseif(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
  19. Toast.makeText(context,"有应用被替换",Toast.LENGTH_LONG).show();
  20. }
  21. /*elseif(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){
  22. Toast.makeText(context,"有应用被重启",Toast.LENGTH_LONG).show();
  23. }*/
  24. /*elseif(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){
  25. Toast.makeText(context,"有应用被安装",Toast.LENGTH_LONG).show();
  26. }*/
  27. }
  28. }

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

具体的源代码如下

  
  1. <receiverandroid:name="getBroadcast"android:enabled="true">
  2. <intent-filter>
  3. <actionandroid:name="android.intent.action.PACKAGE_ADDED"></action>
  4. <!--<actionandroid:name="android.intent.action.PACKAGE_CHANGED"></action>-->
  5. <actionandroid:name="android.intent.action.PACKAGE_REMOVED"></action>
  6. <actionandroid:name="android.intent.action.PACKAGE_REPLACED"></action>
  7. <!--<actionandroid:name="android.intent.action.PACKAGE_RESTARTED"></action>-->
  8. <!--<actionandroid:name="android.intent.action.PACKAGE_INSTALL"></action>-->
  9. <dataandroid:scheme="package"></data>
  10. </intent-filter>
  11. </receiver>

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

package:com.android.myapp

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

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
  2. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  3. Android(安卓)Studio Gradle多渠道打包(动态设定App名称,应用图标
  4. Android选项卡(TabWidget)应用
  5. Android中的AnimationSet使用
  6. android studio运行应用报找不到资源问题
  7. Android学习笔记之mainfest文件中android属性
  8. android 跳转到应用通知设置界面【Android(安卓)8.0 需要特殊处
  9. Android(安卓)广播获取短信内容

随机推荐

  1. android状态栏透明/白底黑字
  2. android转屏处理
  3. 如何实现Android重启应用程序代码 ?
  4. Android之类似于黑名单的短信拦截
  5. swig android的使用
  6. android 复制字符串到剪贴板
  7. android中EditText只允许输入汉字(过滤汉
  8. android反射方式访问内部类成员
  9. Delphi XE5 android 捕获几个事件
  10. Android源码编译全过程