当前位置:操作系统 > 安卓/Android >>

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

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

 

 

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

http://www.zzzyk.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())){
    

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,