Android中获取当前User id的方式

本文介绍了在Android系统中获取当前User ID的两种方法:通过`android.os.Process.myUid()`除以100000得到User ID,以及使用`ActivityManager.getCurrentUser()`获取前台用户ID。前者适用于非系统级别应用,后者适用于涉及用户切换的场景。需要注意,`ActivityManager.getCurrentUser()`需要系统签名权限。
摘要由CSDN通过智能技术生成

Android基于Linux,所以它也是支持多用户的。
当需要获取当前正在运行的user id(int)时,有两种方式:

  1. android.os.Process.myUid()/100000
    /**
     * Returns the identifier of this process's uid.  This is the kernel uid
     * that the process is running under, which is the identity of its
     * app-specific sandbox.  It is different from {@link #myUserHandle} in that
     * a uid identifies a specific app sandbox in a specific user.
     */
    public static final int myUid() {
   
        return Os.getuid(<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值