What is android:sharedUserLabel and what added value does it add on top of android:sharedUserID?

假装没事ソ 提交于 2019-12-23 07:16:38

问题


The documentation (http://developer.android.com/guide/topics/manifest/manifest-element.html#uid) only states I can't use raw strings and the API level it was added, but doesn't explain why I would want to use it. If I already set android:sharedUserID to "com.foo.bar" what value should I put in the string referenced by android:sharedUserLabel, and most importantly why!?

Thank you


回答1:


As far as I understand from the AOSP actually you can use this label just to display a pretty name to a user (if you have several processes in the same uid). For instance, here is a part of code in the RunningState.java file:

    // 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) {
        }
    }

Basically, it does the following things. At first, it tries to get information about the overall process. If it has not find, it tries to get information using UID of the application as a parameter (this is a part of code that I've given here). If there is only one package with this UID the information about the process is got from this package. But if there are several packages (using shareUserId) then it iterates and tries to find official (pretty) name.

As a confirmation to my words I found the following string in MediaProvider:

<!-- Label to show to user for all apps using this UID. -->
<string name="uid_label">Media</string>

Thus, all process that uses android:sharedUserId="android.media" will have name Media.

I do not think that this feature will be used a lot by ordinary developers and is useful for them.



来源:https://stackoverflow.com/questions/9856388/what-is-androidshareduserlabel-and-what-added-value-does-it-add-on-top-of-andro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!