Android获得UID的办法

应用程序在安装时被分配用户 UID, 作为这个应用在Android系统中的唯一标识.
对于普通的应用程序,GID即等于UID.
获取办法有2种.

  1. 通过命令行获取.
1|shell@miki8735_tb_m:/ $ ps | grep hola                                       
u0_a120   26187 360   1757260 97896 SyS_epoll_ 0000000000 S com.hola.weather
shell@miki8735_tb_m:/ $ cat /proc/26187/status                                 
Name:   om.hola.weather
State:  S (sleeping)
Tgid:   26187
Pid:    26187
PPid:   360
TracerPid:  0
Uid:    10120   10120   10120   10120
Gid:    10120   10120   10120   10120
  1. 通过代码获取.



    public static String getUid(Context context) {
        String uid = "";
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo("com.hola.weather", PackageManager.GET_META_DATA);
            uid = String.valueOf(ai.uid);
            JLog.i("ai.uid: " + ai.uid);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return uid;
    }

打印结果:

I/ahking: [ (DeviceInfoUtils.java:162)#GetUid ] ai.uid: 10120

可以看到, 通过这两种方式获取到的uid的值是相同的.

refer to:
https://www.cnblogs.com/FallenHWer/p/3359359.html

你可能感兴趣的:(Android获得UID的办法)