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;
        }
    }
相关推荐
2501_915909061 天前
如何保护 iOS IPA 文件中资源与文件的安全,图片、JSON重命名
android·ios·小程序·uni-app·json·iphone·webview
Root_Hacker1 天前
include文件包含个人笔记及c底层调试
android·linux·服务器·c语言·笔记·安全·php
stevenzqzq1 天前
android flow的背压策略
android·flow
stevenzqzq1 天前
android mvi接口设计1
android·mvi接口设计
stevenzqzq1 天前
android mvi接口设计2
android·mvi接口设计
2501_915909061 天前
原生与 H5 共存情况下的测试思路,混合开发 App 的实际测试场景
android·ios·小程序·https·uni-app·iphone·webview
鸣弦artha1 天前
Flutter框架跨平台鸿蒙开发——Extension扩展方法
android·javascript·flutter
小陈phd1 天前
langGraph从入门到精通(六)——基于 LangGraph 实现结构化输出与智能 Router 路由代理
android·网络·数据库
游戏开发爱好者81 天前
了解 Xcode 在 iOS 开发中的作用和功能有哪些
android·ios·小程序·https·uni-app·iphone·webview
_昨日重现1 天前
Jetpack系列之Compose TopBar
android·android jetpack