获取Android App的Linux UID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6123434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:15:18  来源:igfitidea点击:

Obtain the Linux UID of an Android App

androidlinuxuid

提问by Erez A. Korn

I would like to be able to get the Linux UID (user ID) of an installed Android application.

我希望能够获取已安装的 Android 应用程序的 Linux UID(用户 ID)。

Excerpt from Security and Permissions: "At install time, Android gives each package a distinct Linux user ID. The identity remains constant for the duration of the package's life on that device."

摘自安全和权限:“在安装时,Android 为每个包提供了一个不同的 Linux 用户 ID。该身份在包在该设备上的生命周期内保持不变。”

Is there a way to retrieve this UID?

有没有办法检索这个UID?

采纳答案by CommonsWare

Use PackageManagerand getApplicationInfo().

使用PackageManagergetApplicationInfo()

回答by pavan

  • The ?packages.xmlfile present in /data/system
  • The packages.listfile present in /data/system
  • 这 ?packages.xml文件存在于/data/system
  • packages.list文件存在于/data/system

Contain the list of applications installed and their corresponding UID's.

包含已安装的应用程序列表及其相应的 UID。

回答by Joe Bowbeer

adb shell dumpsys package com.example.myapp | grep userId=

adb shell dumpsys package com.example.myapp | grep userId=

回答by Ege Kuzubasioglu

PackageManager packageManager = getPackageManager();
try {
    applicationId = String.valueOf(packageManager.getApplicationInfo("com.example.app", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

回答by Matt Ke

As CommonsWare already wrote, you can use PackageManagerto get the UID.

正如CommonsWare 已经写的那样,您可以使用它PackageManager来获取UID。

Here's an example:

下面是一个例子:

int uid;
try {
    ApplicationInfo info = context.getPackageManager().getApplicationInfo(
            context.getPackageName(), 0);
    uid = info.uid;
} catch (PackageManager.NameNotFoundException e) {
    uid = -1;
}
Log.i(LOG_TAG, "UID = " + uid);

回答by lyind

Use android.os.Process.myUid()to get the calling apps UID directly.

用于android.os.Process.myUid()直接获取调用应用程序 UID。

Using the PackageManager is not necessary to find the own UID.

不需要使用 PackageManager 来查找自己的 UID。