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

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

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 

两种方法:

1、代码

1, Define a class extends BroadcastReceiver;
2, Register broadcast receiver;
UninstallApp receiver = new UninstallApp();
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_INSTALL);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package"); //This line is very important. Otherwise, broadcast can't be received.这一行很得要
registerReceiver(receiver, filter);


2、XML

<data android:scheme="package"></data> This line is very important. Otherwise, broadcast can't be received. 这一行很重要

<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>


import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.util.Log;  
  
public class UninstallApp extends BroadcastReceiver {  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Log.e("intent", "intent="+intent.getAction());
	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();  
            }*/ 
    }  
  
}  

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

      package:com.android.myapp


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值