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);
	}
相关推荐
音视频牛哥25 分钟前
Android 平台RTSP/RTMP播放器SDK接入说明
android·音视频·大牛直播sdk·rtsp播放器·rtmp播放器·rtmp低延迟播放·rtmpplayer
aningxiaoxixi1 小时前
Android Framework 之 AudioDeviceBroker
android·windows·ffmpeg
~Yogi1 小时前
今日学习:工程问题(场景题)
android·学习
奔跑吧 android2 小时前
【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】
android·bluetooth·bt·aosp13
移动开发者1号2 小时前
Android Activity状态保存方法
android·kotlin
移动开发者1号2 小时前
Volley源码深度分析与设计亮点
android·kotlin
张风捷特烈2 小时前
每日一题 Flutter#7,8 | 关于 State 两道简答题
android·flutter·面试
计蒙不吃鱼10 小时前
一篇文章实现Android图片拼接并保存至相册
android·java·前端
LucianaiB11 小时前
如何做好一份优秀的技术文档:专业指南与最佳实践
android·java·数据库
duwei_wang16 小时前
[Android]-Admob配置过多导致的慢消息
android