Android编程监听APK安装与删除等过程的方法

作者:袖梨 2022-06-25

本文实例讲述了Android编程监听APK安装与删除等过程的方法。分享给大家供大家参考,具体如下:

软件下载后的一系列动作监听:先前是通过Service监听扫描获取状态,以后用这个方法测试使用

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();
      }*/
    }
}



  
    
      
        
        
      
    
   
     
     
     
     
     
     
    
      
       

  
  


代码实现添加:

private final BroadcastReceiver apkInstallListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
      System.out.println("**************Broadcase*************");
      File file = uninstallApk.get(isDeleted);
      System.out.println(file.toString()+"*****");
       file.delete();
        //System.out.println(uninstallApk.size()+"(*******"+uApks.size());
       if(uninstallApk!=null&&uApks!=null)
       {
       uninstallApk.remove(isDeleted);
       uApks.remove(isDeleted);
       }
      //清除集合里面的值
        if(uninstallApk!=null)
        {
         System.out.println("onpause******"+uninstallApk.size());
         uninstallApk.clear();
        }
        if(uApks!=null)
        {
         uApks.clear();
        }
          System.out.println("******应用添加***"+isDeleted);
       Toast.makeText(context, "有应用被添加"+isDeleted, Toast.LENGTH_LONG).show();
      }
        else if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
          System.out.println("*****应用被删除");
         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())){
          System.out.println("****应用被替换");
         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();
      }*/
  }
};
// 注册监听
private void registerSDCardListener(){
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    intentFilter.addDataScheme("package");
    registerReceiver(apkInstallListener, intentFilter);
}

java里的调用 registerSDCardListener()

@Override
protected void onDestroy()
{
 super.onDestroy();
 //unregisterReceiver(apkInstallListener);
}

相关文章

精彩推荐