Android framework 编程之 - Binder调用方UID

注:不要弄混 uid 和 userID 的区别。

1. 获取Binder 调用方UID

java 复制代码
	Binder.getCallingUid

    /**
     * Return the Linux UID assigned to the process that sent you the
     * current transaction that is being processed. This UID can be used with
     * higher-level system services to determine its identity and check
     * permissions. If the current thread is not currently executing an
     * incoming transaction, then its own UID is returned.
     */
    @CriticalNative
    @android.ravenwood.annotation.RavenwoodReplace
    public static final native int getCallingUid();

2. 判断UID 是否是特殊UID

例如判断是否是System UID

java 复制代码
uid == Process.SYSTEM_UID

特殊 UID 的列表

这里仅仅列出一部分

frameworks/base/core/java/android/os/Process.java

java 复制代码
    /**
     * An invalid UID value.
     */
    public static final int INVALID_UID = -1;

    /**
     * Defines the root UID.
     */
    public static final int ROOT_UID = 0;

    /**
     * Defines the UID/GID under which system code runs.
     */
    public static final int SYSTEM_UID = 1000;

    /**
     * Defines the UID/GID under which the telephony code runs.
     */
    public static final int PHONE_UID = 1001;

    /**
     * Defines the UID/GID for the user shell.
     */
    public static final int SHELL_UID = 2000;

将 Binder 调用方 UID 变为本地 UID ,豁免部分权限检查

java 复制代码
	// 将调用标识变为本地进程,相对于System Server 就是 system server 进程
	final long token = Binder.clearCallingIdentity();
	try {
		// 权限检查的是本地进程。
	} finally {
		// 将调用标识变为非本地标识
		Binder.restoreCallingIdentity(token);
	}
相关推荐
shaoming37761 小时前
检查系统硬件配置是否满足PyCharm最低要求
android·spring boot·mysql
一起搞IT吧2 小时前
高通Camx功能feature分析之十五:insensor zoom介绍及实现
android·智能手机·相机
aqi003 小时前
一文读懂 HarmonyOS 6.1 带来的十大重要升级
android·华为·harmonyos·鸿蒙·harmony
秋95 小时前
MySQL 9.7.0 使用详解:新特性、实战与避坑指南
android·数据库·mysql
狼与自由5 小时前
clickhouse ReplacingMergeTree
android·clickhouse
吉吉615 小时前
php反序列化基础知识前奏
android·php·反序列化
努力努力再努力wz6 小时前
【MySQL进阶系列】拒绝冗余SQL:带你透彻理解视图的底层逻辑
android·c语言·数据结构·数据库·c++·sql·mysql
常利兵6 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
android·科技
_李小白7 小时前
【android opencv学习笔记】Day 5: 高效的图像扫描
android·opencv·学习
liang_jy14 小时前
Android 窗口容器树(一)—— 窗口和窗口容器树
android·源码