文档 ( http://developer.android.com/guide/topics/manifest/manifest-element.html#uid ) 只说明我不能使用原始字符串和它添加的 API 级别,但没有解释我为什么要使用它。 如果我已经将 android:sharedUserID 设置为“com.foo.bar”,我应该在 android:sharedUserLabel 引用的字符串中放入什么值,最重要的是为什么!?
谢谢
最佳答案
据我从 AOSP 了解到,实际上你可以使用这个标签来向用户显示一个漂亮的名字(如果你在同一个 uid 中有多个进程)。例如,这是 RunningState.java 文件中的部分代码:
// If we couldn't get information about the overall
// process, try to find something about the uid.
String[] pkgs = pm.getPackagesForUid(mUid);
// If there is one package with this uid, that is what we want.
if (pkgs.length == 1) {
try {
ApplicationInfo ai = pm.getApplicationInfo(pkgs[0], 0);
mDisplayLabel = ai.loadLabel(pm);
mLabel = mDisplayLabel.toString();
mPackageInfo = ai;
return;
} catch (PackageManager.NameNotFoundException e) {
}
}
// If there are multiple, see if one gives us the official name
// for this uid.
for (String name : pkgs) {
try {
PackageInfo pi = pm.getPackageInfo(name, 0);
if (pi.sharedUserLabel != 0) {
CharSequence nm = pm.getText(name,
pi.sharedUserLabel, pi.applicationInfo);
if (nm != null) {
mDisplayLabel = nm;
mLabel = nm.toString();
mPackageInfo = pi.applicationInfo;
return;
}
}
} catch (PackageManager.NameNotFoundException e) {
}
}
基本上,它会做以下事情。首先,它会尝试获取有关整个过程的信息。如果没有找到,它会尝试使用应用程序的 UID 作为参数来获取信息(这是我在此处给出的代码的一部分)。如果只有一个包具有此 UID,则有关进程的信息将从该包中获取。但是如果有多个包(使用 shareUserId),那么它会迭代并尝试找到正式的(漂亮的)名称。
作为对我的话的证实,我在 MediaProvider 中发现了以下字符串:
<!-- Label to show to user for all apps using this UID. -->
<string name="uid_label">Media</string>
因此,所有使用 android:sharedUserId="android.media"
的进程都将具有名称 Media
。
我不认为这个特性会被普通开发者大量使用并且对他们有用。
关于android - 什么是android :sharedUserLabel and what added value does it add on top of android:sharedUserID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9856388/