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);
	}
相关推荐
Dnelic-25 分钟前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen3 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年10 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿12 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神14 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛14 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法14 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter16 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快17 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl17 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5