gpt4 book ai didi

java - 如何判断我是否在 android :isolatedProcess? 中运行

转载 作者:行者123 更新时间:2023-12-01 13:29:21 46 4
gpt4 key购买 nike

我正在开发适用于 Android 的第三方库,并且需要能够判断我是作为具有所需权限的特权进程运行,还是生活在 AndroidManifest.xml 中定义的具有受限权限的隔离进程中文件:

<service android:name="mySandboxedService" android:permission="com.android.chrome.permission.CHILD_SERVICE" android:exported="false" android:process=":sandboxed_process0" android:isolatedProcess="true" />

原因是我正在尝试做的某些事情,例如获取正在运行的应用程序进程的数量(以及其他各种事情),如果它们被隔离,将引发 RuntimeException。此代码如果不作为隔离进程运行将成功运行,但如果进程隔离则将抛出 RTE:
    ActivityManager aM = (ActivityManager) context.getSystemService(android.content.Context.ACTIVITY_SERVICE);

List<ActivityManager.RunningAppProcessInfo> l = null;

try {
l = aM.getRunningAppProcesses();
} catch (RuntimeException e) {
Log.w(LOGTAG, "Isolated process not allowed allowed to call getRunningAppProcesses, cannot get number of running apps.");
}

从我的日志猫:
java.lang.SecurityException: Isolated process not allowed to call getRunningAppProcesses

有谁知道我可以检查当前进程以查看它是隔离的还是特权的方法?我检查了 Android Service doc here,它没有提供太多信息。

我的最终目标是从主特权线程初始化我的应用程序一次,然后忽略来自可能创建的各种沙盒进程的所有启动调用。我不想在这些中运行,但我的钩子(Hook)在 Application.onCreate 中,并且被每个进程调用,无论是否沙盒。

我考虑过将这些检查之一添加到我的初始化中并在 RTE 被抛出时捕获它的想法。但如果它有一个公共(public) API,我宁愿使用它。

最佳答案

可以查看正在运行的进程的uid,看看它们是否属于隔离进程的范围。

int AID_ISOLATED_START = 99000;
int AID_ISOLATED_END = 99999;
int uid = Process.myUid();
if (uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END) {
Log.i(TAG, "This is from an isolated process");
}
工艺范围信息来源: https://android.googlesource.com/platform/system/sepolicy/+/master/public/isolated_app.te
编辑:以上内容在 Android 8.1 及更低版本上被发现是不可靠的。
另一种方法是尝试访问特权 API 并查看是否引发异常。
try {
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE);
activityManager.getRunningAppProcesses();
} catch (SecurityException e) {
Log.i(TAG, "This is from an isolated process");
}
正如在另一个答案中所指出的,Android Pie/9 (API 28) 为此引入了一个新的 API。见 https://developer.android.com/reference/android/os/Process#isIsolated() .
这似乎存在于低至 Android 4.3 的版本中。见 https://cs.android.com/android/platform/superproject/+/android-4.3.1_r1:frameworks/base/core/java/android/os/Process.java;l=680

关于java - 如何判断我是否在 android :isolatedProcess? 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474833/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com