本文主要讨论如何监控 Android 程序包的安装和删除


Android系统的安装方式我在 《Android 应用程序安装方式 的详细调研》一文中已经做了详细的阐述,链接如下
http://www.2cto.com/kf/201201/116677.html


基于这些安装方式,我们如何对系统进行的安装进行监控呢?

通过阅读Android SDK里关于intent.action这部分里面的描述,我们可以找到一些与package相关的系统广播
[c-sharp] android.intent.action.PACKAGE_ADDED
android.intent.action.PACKAGE_CHANGED
android.intent.action.PACKAGE_DATA_CLEARED
android.intent.action.PACKAGE_INSTALL
android.intent.action.PACKAGE_REMOVED
android.intent.action.PACKAGE_REPLACED
android.intent.action.PACKAGE_RESTARTED
android.intent.action.PACKAGE_ADDED
android.intent.action.PACKAGE_CHANGED
android.intent.action.PACKAGE_DATA_CLEARED
android.intent.action.PACKAGE_INSTALL
android.intent.action.PACKAGE_REMOVED
android.intent.action.PACKAGE_REPLACED
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 应用程序的安装和删除

详细的实现代码如下

getBroadcast.java[c-sharp] package zy.Broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class getBroadcast extends BroadcastReceiver {         @Override         public void onReceive(Context context, Intent intent) {                                   if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){                     Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();             }                 else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){                     Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();             }              /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show();            }*/                 else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){                     Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();             }                /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show();            }*/               /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){                    Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show();            }*/                      }         } 



package zy.Broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class getBroadcast extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {                                if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();            }                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();            }             /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show();            }*/                else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();            }               /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){                    Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show();            }*/              /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){                    Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show();            }*/                   }      }


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

     </manifest>  <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="zy.Broadcast"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".Broadcast"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>      <receiver android:name="getBroadcast" android:enabled="true" >         <intent-filter>          <action android:name="android.intent.action.PACKAGE_ADDED"></action>          <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->          <action android:name="android.intent.action.PACKAGE_REMOVED"></action>          <action android:name="android.intent.action.PACKAGE_REPLACED"></action>          <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->        <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->            <data android:scheme="package"></data>              </intent-filter></receiver>    </application>    <uses-sdk android:minSdkVersion="7" />   </manifest> 


把程序安装之后 ,系统就会注册这个BroadcastReceiver
然后有应用安装删除替换操作时时,就会弹出Toast提示
以上这样,我们就可以实现监控Android 应用程序的安装过程

至于拦截安装过程,我也正在研究中,大家有好的idea可以与我 分享,谢谢
摘自 Zy的技术心得

更多相关文章

  1. Android热点回顾第一期
  2. Android简介与开发环境搭建
  3. Android的进程,线程模型
  4. Android应用程序启动过程源代码分析
  5. Android开发环境的搭建
  6. Android(安卓)应用架构组件(Architecture Components)实践
  7. Android(安卓)NDK环境搭建
  8. HelloWorld-----Google手机操作系统Android应用开发入门
  9. 漫谈Android安全框架

随机推荐

  1. 某某某NAS网络储存灾备一体化项目
  2. 应对未来物联网大潮:如何在内存有限的情况
  3. 这可能是AI、机器学习和大数据领域覆盖最
  4. 函数装饰器
  5. 实例ES6演示数组,对象,传参解构; 实例演示
  6. 游戏服务器和Web服务器的区别
  7. Authing 客户故事|句子互动
  8. 工具之_vim
  9. vim 常用操作
  10. 中国学生发现1000行Python代码脚本中的bu