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

来源:互联网 发布:手机音乐编辑软件 编辑:程序博客网 时间:2024/06/05 21:10

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

 

 

Android系统的安装方式我在 《Android 应用程序安装方式 的详细调研》一文中已经做了详细的阐述,链接如下

http://blog.csdn.net/Zengyangtech/archive/2010/07/15/5737522.aspx 

 

 

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

 

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

[c-sharp] view plaincopy
  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 应用程序的安装和删除

 

详细的实现代码如下

getBroadcast.java

[c-sharp] view plaincopy
  1. package zy.Broadcast;  
  2. import android.content.BroadcastReceiver;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.widget.Toast;  
  6. public class getBroadcast extends BroadcastReceiver {  
  7.         @Override  
  8.         public void onReceive(Context context, Intent intent) {  
  9.                  
  10.                   if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){  
  11.                     Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();  
  12.             }  
  13.                 else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){  
  14.                     Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();  
  15.             }  
  16.              /*   else  if(Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())){ 
  17.                     Toast.makeText(context, "有应用被改变", Toast.LENGTH_LONG).show(); 
  18.             }*/  
  19.                 else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){  
  20.                     Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();  
  21.             }  
  22.                /* else  if(Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())){ 
  23.                     Toast.makeText(context, "有应用被重启", Toast.LENGTH_LONG).show(); 
  24.             }*/  
  25.               /*  else  if(Intent.ACTION_PACKAGE_INSTALL.equals(intent.getAction())){ 
  26.                     Toast.makeText(context, "有应用被安装", Toast.LENGTH_LONG).show(); 
  27.             }*/  
  28.               
  29.         }  
  30.          
  31. }  
 

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

具体的源代码如下

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="zy.Broadcast"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".Broadcast"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.       <receiver android:name="getBroadcast" android:enabled="true" >  
  15.          <intent-filter>  
  16.              <action android:name="android.intent.action.PACKAGE_ADDED"></action>  
  17.              <!-- <action android:name="android.intent.action.PACKAGE_CHANGED"></action>-->  
  18.              <action android:name="android.intent.action.PACKAGE_REMOVED"></action>  
  19.              <action android:name="android.intent.action.PACKAGE_REPLACED"></action>  
  20.              <!-- <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>-->  
  21.            <!--    <action android:name="android.intent.action.PACKAGE_INSTALL"></action>-->  
  22.                <data android:scheme="package"></data>  
  23.               </intent-filter>  
  24. </receiver>  
  25.     </application>  
  26.     <uses-sdk android:minSdkVersion="7" />  
  27.       
  28. </manifest>   
 

 

 

把程序安装之后 ,系统就会注册这个BroadcastReceiver

然后有应用安装删除替换操作时时,就会弹出Toast提示

 

删除应用

 

添加应用

 

有应用被替换

 

 

以上这样,我们就可以实现监控Android 应用程序的安装过程

 

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