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);
	}
相关推荐
00后程序员张17 小时前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
柳岸风19 小时前
Android Studio Meerkat | 2024.3.1 Gradle Tasks不展示
android·ide·android studio
编程乐学19 小时前
安卓原创--基于 Android 开发的菜单管理系统
android
whatever who cares21 小时前
android中ViewModel 和 onSaveInstanceState 的最佳使用方法
android
毕设源码-钟学长21 小时前
【开题答辩全过程】以 Android的传统中医诊断管理系统为例,包含答辩的问题和答案
android
脚踏实地,坚持不懈!21 小时前
Android,Jetpack Compose,坦克大战游戏案例Demo
android·游戏
yzpyzp1 天前
kotlin的函数前面增加suspend关键字的作用
android·开发语言·kotlin
jiet_h1 天前
Android Kotlin ObjectAnimator 和 ValueAnimator 全面解析
android·开发语言·kotlin
Android技术之家1 天前
Kotlin与Compose:Android开发的现代化变革
android·java·开发语言·kotlin
诸神黄昏EX1 天前
Android SystemServer 系列专题【篇五:UserController用户状态控制】
android