Android15 安装APK时监听且替换安装

有些特殊需求,需要监听安装的APK,当监听到这个APK安装的时候,替换掉它,安装已经预置的APK,修改如下:

1.在framework/base/services/core/java/com/android/server/pm/InstallingSession.java里面

修改:

复制代码
diff --git a/base/services/core/java/com/android/server/pm/InstallingSession.java b/base/services/core/java/com/android/server/pm/InstallingSession.java
index 3d8f2bbf4..5d7dfac94 100644
--- a/base/services/core/java/com/android/server/pm/InstallingSession.java
+++ b/base/services/core/java/com/android/server/pm/InstallingSession.java
@@ -64,6 +64,10 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
+import android.content.Intent;
+import android.os.SystemProperties;
+import java.util.HashSet;
+
 class InstallingSession {
     final OriginInfo mOriginInfo;
     final MoveInfo mMoveInfo;
@@ -251,6 +255,18 @@ class InstallingSession {
         PackageInfoLite pkgLite = PackageManagerServiceUtils.getMinimalPackageInfo(mPm.mContext,
                 mPackageLite, mOriginInfo.mResolvedPath, mInstallFlags, mPackageAbiOverride);
 
+        //patch add 新增:检查是否为GMS包,若是则直接返回失败
+        if (pkgLite != null && isGmsPackage(pkgLite.packageName)) {
+           mRet = PackageManager.INSTALL_FAILED_INTERNAL_ERROR; //标记安装被阻断
+           //request.setError(mRet, "GMS package update is not allowed: " + pkgLite.packageName);
+           Slog.w(TAG, "Blocked GMS package installation: " + pkgLite.packageName);
+           if("com.example".equals(pkgLite.packageName)){
+               mPm.mContext.sendBroadcast(new Intent("com.action.intercept.example"));
+           }
+           return; // 终止后续安装流程
+        }
+               //patch add end
+               
         // For staged session, there is a delay between its verification and install. Device
         // state can change within this delay and hence we need to re-verify certain conditions.
         boolean isStaged = (mInstallFlags & INSTALL_STAGED) != 0;
@@ -284,6 +300,21 @@ class InstallingSession {
         }
     }
 
+     // 新增方法:判断是否为GMS相关包
+    private boolean isGmsPackage(String packageName) {
+       // GMS核心包名列表,可根据实际需要扩展
+       Set<String> gmsPackageNames = new HashSet<>();
+       gmsPackageNames.add("com.google.android.gms"); // GMS核心服务
+       if ("on".equals(SystemProperties.get("persist.rp.ban.example","off"))){
+           gmsPackageNames.add("com.example");
+       }
+       //gmsPackageNames.add("com.google.android.gms.pano"); // GMS全景组件
+       //gmsPackageNames.add("com.google.android.gsf"); // Google服务框架
+       //gmsPackageNames.add("com.android.vending"); // Google Play商店(若需拦截其更新)
+       // 可添加其他GMS相关包名,如地图、YouTube等
+       return gmsPackageNames.contains(packageName);
+    }
+       
     private void handleReturnCode(InstallRequest installRequest) {
         processPendingInstall(installRequest);
     }

这里是当检测到安装GMS相关的环境检测应用的时候,终止安装,发送自定广播安装自定义应用,替换当前检测应用。

2.安装应用

收到广播后可以使用Runtime.getRuntime().exec安装预置的应用

java 复制代码
public boolean silenceInstallApk( String path){
        try {
            Runtime.getRuntime().exec("pm install -r "+path);
            return true;
        } catch (Exception e) {
           return false;
        }
    }
相关推荐
kururunga22 分钟前
Android知识点回顾之蓝牙协议
android·蓝牙·车载开发
StarsGreen1 小时前
aosp中的SELinux 策略配置和编译流程
android
方白羽5 小时前
Android 开发中常见的编译加速方案
android·app·gradle
StarsGreen8 小时前
Android VINTF 中 Device Manifest 与 Framework Manifest 详解
android
Name_NaN_None10 小时前
安卓手机远程控制电脑方法(Android/Windows) —— 「小白教程」
android·windows·智能手机·电脑·笔记本
恋猫de小郭10 小时前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
敢敢のwings20 小时前
GR00T 2:当机器人学会先想象未来,再决定怎么动
android·机器人
传奇开心果编程20 小时前
【Jetpack Compose娓娓道来】第55课:传感器与设备姿态——把物理世界的数据接进Compose
android·kotlin·移动开发·传感器·jetpack compose·ui框架·声明式ui
传奇开心果编程21 小时前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Compose
android·kotlin·移动开发·蓝牙·jetpack compose·声明式ui·位置权限
alexhilton1 天前
卡顿定位:从 UI 线程到 RenderThread
android·kotlin·android jetpack