小纪爱做梦 發表於 2023-5-8 11:12:00

Android系统开发 获取userId

<h1><span style="color: rgba(22, 145, 121, 1)">前言</span></h1>
<p>  userId是Android4.2之后的版本的新功能,多用户概念下的产物。用来确定当前使用设备的用户id。此功能可以在原生设置-系统-高级-多用户中查看到。如果你想验证id的变化可以通过创建新用户后切换用户。</p>
<p>  一般应用开发的情况下,我们是不会使用到这种功能的。但是在系统开发的情况下处理设置相关功能会涉及到此功能。一些framework库与settingslib库的api需要我们传入当前用户的userId。userId一共有三种获取方式,下面会一一举例</p>
<h1><span style="color: rgba(22, 145, 121, 1)">通过反射UserHandle的系统方法myUserId获得</span></h1>
<pre class="highlighter-hljs" data-dark-theme="true"><code>public static int getCurrentUserId() {
    if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
      try {
            @SuppressLint("DiscouragedPrivateApi") final Method m = android.os.UserHandle.class.getDeclaredMethod("myUserId");
            m.setAccessible(true);
            final Object o = m.invoke(null);
            if (o instanceof Integer) {
                return (Integer) o;
            }
      } catch (Exception ignored) {
      }
    }
    return -1;
}</code></pre>
<h1><span style="color: rgba(22, 145, 121, 1)">通过反射ActivityManager的getCurrentUser方法获取</span></h1>
<p><strong><span style="color: rgba(186, 55, 42, 1)">注意!</span></strong>getCurrentUser方法是系统级API,所以反射此方法需要系统签名</p>
<pre class="highlighter-hljs" data-dark-theme="true"><code>public static int getCurrentUserId2() {
    try {
      @SuppressLint("DiscouragedPrivateApi") final Method m = android.app.ActivityManager.class.getDeclaredMethod("getCurrentUser");
      m.setAccessible(true);
      final Object o = m.invoke(null);
      if (o instanceof Integer) {
            return (Integer) o;
      }
    } catch (Exception ignored) {
    }
    return -1;
}</code></pre>
<h1><span style="color: rgba(22, 145, 121, 1)">最简单的方法通过android.os.Process获取</span></h1>
<pre class="highlighter-hljs" data-dark-theme="true"><code>public static int getCurrentUserId3(){
    return android.os.Process.myUid()/100000;
}</code></pre>
<p><span style="color: rgba(0, 0, 0, 1)">这方式感觉有点莫名其妙?为什么要除以100000?其实只要阅读一下UserHandle的myUserId源码立刻能明白,下面贴出源码。 但是,个人是建议反射UserHandle获得userId,因为怕asop搞幺蛾子在后续版本上改掉数值。</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">源码1</span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><img src="https://img2023.cnblogs.com/blog/1497956/202305/1497956-20230508111123734-216779052.png"></span></p>
<p><span style="color: rgba(0, 0, 0, 1)">源码2</span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><img src="https://img2023.cnblogs.com/blog/1497956/202305/1497956-20230508111133914-1767535283.png"></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><img src="https://img2023.cnblogs.com/blog/1497956/202305/1497956-20230508111201268-1581611352.png"></span></p>
<p>&nbsp;</p>
<p><span style="color: rgba(22, 145, 121, 1)">End</span></p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="text-align: center">
    <p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/17381176.html </p>
    <div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/17381176.html
頁: [1]
查看完整版本: Android系统开发 获取userId