2012-03-24 87 views
10

该文档(http://developer.android.com/guide/topics/manifest/manifest-element.html#uid)只声明我不能使用原始字符串和它添加的API级别,但不能解释为什么我想要使用它。 如果我已经将android:sharedUserID设置为“com.foo.bar”,我应该在由android:sharedUserLabel引用的字符串中放入什么值,最重要的是为什么!什么是android:sharedUserLabel以及它在android:sharedUserID之上添加了什么附加值?

谢谢

回答

7

据我了解从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所有过程。

我不认为这个功能会被普通开发者使用很多,对他们很有用。

相关问题