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;
        }
    }
相关推荐
李艺为4 小时前
Fake Device Test作假屏幕分辨率分析
android·java
zh_xuan4 小时前
github远程library仓库升级
android·github
峥嵘life4 小时前
Android蓝牙停用绝对音量原理
android
czlczl200209255 小时前
IN和BETWEEN在索引效能的区别
android·adb
Volunteer Technology6 小时前
ES高级搜索功能
android·大数据·elasticsearch
北京自在科技6 小时前
Find Hub App 小更新
android·ios·安卓·findmy·airtag
lbb 小魔仙6 小时前
2026远程办公软件夏季深度横测:ToDesk、向日葵、网易UU远程全面对比,远控白皮书
android·服务器·网络协议·tcp/ip·postgresql
coding_fei6 小时前
AudioServer初始化过程
android
brucelee1867 小时前
Docker 运行 Android 模拟器
android·docker·容器
私人珍藏库7 小时前
[Android] 小柚市场app v2.3.0.8安卓版TV版
android