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

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

 

 

ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)
ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本
ACTION_PACKAGE_CHANGED 一个已存在的应用程序包已经改变,包括包名
ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)
ACTION_PACKAGE_RESTARTED 用户重新开始一个包,包的所有进程将被杀死,所有与其联系的运行时间状态应该被移除,包括包名(重新开始包程序不能接收到这个广播)
ACTION_PACKAGE_DATA_CLEARED 用户已经清楚一个包的数据,包括包名(清除包程序不能接收到这个广播)

代码实现 

在AndroidManifest.xml中定义广播

复制代码
<receiver android:name=".AppInstallReceiver"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" /> 
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>
复制代码

这里选用的是

ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)
ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本
ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)

再看AppInstallReceiver 

复制代码
public class AppInstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        PackageManager manager = context.getPackageManager();
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, "安装成功"+packageName, Toast.LENGTH_LONG).show();
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, "卸载成功"+packageName, Toast.LENGTH_LONG).show();
        }
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            String packageName = intent.getData().getSchemeSpecificPart();
            Toast.makeText(context, "替换成功"+packageName, Toast.LENGTH_LONG).show();
        }
        

    }

}
复制代码

 

 

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

 

通过阅读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>即可在系统里捕获这些广播消息

具体的源代码如下

[c-sharp] <?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>  
<?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的技术心得

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值