Android 的manifest解析

AndroidManifest.xml采用xml文件格式描述,使用xml标签的形式描述每个属性,AndroidManifest.xml的解析也遵循xml解析的规律,对AndroidManifest.xml的解析主要用到PackageManager,PackageInfo,ApplicationInfo三个类,PackageInfo描述了应用级的信息,包括应用的版本号,版本名称,对应AndroidManifest.xml最外层的信息,ApplicationInfo描述了<application></application>标签之间的信息,包括声明的meta-data和activity信息,

PackageInfo.java源码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package android.content.pm;  
  18.   
  19. import android.os.Parcel;  
  20. import android.os.Parcelable;  
  21.   
  22. /** 
  23.  * Overall information about the contents of a package.  This corresponds 
  24.  * to all of the information collected from AndroidManifest.xml. 
  25.  */  
  26. public class PackageInfo implements Parcelable {  
  27.     /** 
  28.      * The name of this package.  From the <manifest> tag's "name" 
  29.      * attribute. 
  30.      */  
  31.     public String packageName;  
  32.   
  33.     /** 
  34.      * The version number of this package, as specified by the <manifest> 
  35.      * tag's {@link android.R.styleable#AndroidManifest_versionCode versionCode} 
  36.      * attribute. 
  37.      */  
  38.     public int versionCode;  
  39.       
  40.     /** 
  41.      * The version name of this package, as specified by the <manifest> 
  42.      * tag's {@link android.R.styleable#AndroidManifest_versionName versionName} 
  43.      * attribute. 
  44.      */  
  45.     public String versionName;  
  46.       
  47.     /** 
  48.      * The shared user ID name of this package, as specified by the <manifest> 
  49.      * tag's {@link android.R.styleable#AndroidManifest_sharedUserId sharedUserId} 
  50.      * attribute. 
  51.      */  
  52.     public String sharedUserId;  
  53.       
  54.     /** 
  55.      * The shared user ID label of this package, as specified by the <manifest> 
  56.      * tag's {@link android.R.styleable#AndroidManifest_sharedUserLabel sharedUserLabel} 
  57.      * attribute. 
  58.      */  
  59.     public int sharedUserLabel;  
  60.       
  61.     /** 
  62.      * Information collected from the <application> tag, or null if 
  63.      * there was none. 
  64.      */  
  65.     public ApplicationInfo applicationInfo;  
  66.       
  67.     /** 
  68.      * The time at which the app was first installed.  Units are as 
  69.      * per {@link System#currentTimeMillis()}. 
  70.      */  
  71.     public long firstInstallTime;  
  72.   
  73.     /** 
  74.      * The time at which the app was last updated.  Units are as 
  75.      * per {@link System#currentTimeMillis()}. 
  76.      */  
  77.     public long lastUpdateTime;  
  78.   
  79.     /** 
  80.      * All kernel group-IDs that have been assigned to this package. 
  81.      * This is only filled in if the flag {@link PackageManager#GET_GIDS} was set. 
  82.      */  
  83.     public int[] gids;  
  84.   
  85.     /** 
  86.      * Array of all {@link android.R.styleable#AndroidManifestActivity 
  87.      * <activity>} tags included under <application>, 
  88.      * or null if there were none.  This is only filled in if the flag 
  89.      * {@link PackageManager#GET_ACTIVITIES} was set. 
  90.      */  
  91.     public ActivityInfo[] activities;  
  92.   
  93.     /** 
  94.      * Array of all {@link android.R.styleable#AndroidManifestReceiver 
  95.      * <receiver>} tags included under <application>, 
  96.      * or null if there were none.  This is only filled in if the flag 
  97.      * {@link PackageManager#GET_RECEIVERS} was set. 
  98.      */  
  99.     public ActivityInfo[] receivers;  
  100.   
  101.     /** 
  102.      * Array of all {@link android.R.styleable#AndroidManifestService 
  103.      * <service>} tags included under <application>, 
  104.      * or null if there were none.  This is only filled in if the flag 
  105.      * {@link PackageManager#GET_SERVICES} was set. 
  106.      */  
  107.     public ServiceInfo[] services;  
  108.   
  109.     /** 
  110.      * Array of all {@link android.R.styleable#AndroidManifestProvider 
  111.      * <provider>} tags included under <application>, 
  112.      * or null if there were none.  This is only filled in if the flag 
  113.      * {@link PackageManager#GET_PROVIDERS} was set. 
  114.      */  
  115.     public ProviderInfo[] providers;  
  116.   
  117.     /** 
  118.      * Array of all {@link android.R.styleable#AndroidManifestInstrumentation 
  119.      * <instrumentation>} tags included under <manifest>, 
  120.      * or null if there were none.  This is only filled in if the flag 
  121.      * {@link PackageManager#GET_INSTRUMENTATION} was set. 
  122.      */  
  123.     public InstrumentationInfo[] instrumentation;  
  124.       
  125.     /** 
  126.      * Array of all {@link android.R.styleable#AndroidManifestPermission 
  127.      * <permission>} tags included under <manifest>, 
  128.      * or null if there were none.  This is only filled in if the flag 
  129.      * {@link PackageManager#GET_PERMISSIONS} was set. 
  130.      */  
  131.     public PermissionInfo[] permissions;  
  132.       
  133.     /** 
  134.      * Array of all {@link android.R.styleable#AndroidManifestUsesPermission 
  135.      * <uses-permission>} tags included under <manifest>, 
  136.      * or null if there were none.  This is only filled in if the flag 
  137.      * {@link PackageManager#GET_PERMISSIONS} was set.  This list includes 
  138.      * all permissions requested, even those that were not granted or known 
  139.      * by the system at install time. 
  140.      */  
  141.     public String[] requestedPermissions;  
  142.       
  143.     /** 
  144.      * Array of flags of all {@link android.R.styleable#AndroidManifestUsesPermission 
  145.      * <uses-permission>} tags included under <manifest>, 
  146.      * or null if there were none.  This is only filled in if the flag 
  147.      * {@link PackageManager#GET_PERMISSIONS} was set.  Each value matches 
  148.      * the corresponding entry in {@link #requestedPermissions}, and will have 
  149.      * the flags {@link #REQUESTED_PERMISSION_REQUIRED} and 
  150.      * {@link #REQUESTED_PERMISSION_GRANTED} set as appropriate. 
  151.      */  
  152.     public int[] requestedPermissionsFlags;  
  153.   
  154.     /** 
  155.      * Flag for {@link #requestedPermissionsFlags}: the requested permission 
  156.      * is required for the application to run; the user can not optionally 
  157.      * disable it.  Currently all permissions are required. 
  158.      */  
  159.     public static final int REQUESTED_PERMISSION_REQUIRED = 1<<0;  
  160.   
  161.     /** 
  162.      * Flag for {@link #requestedPermissionsFlags}: the requested permission 
  163.      * is currently granted to the application. 
  164.      */  
  165.     public static final int REQUESTED_PERMISSION_GRANTED = 1<<1;  
  166.   
  167.     /** 
  168.      * Array of all signatures read from the package file.  This is only filled 
  169.      * in if the flag {@link PackageManager#GET_SIGNATURES} was set. 
  170.      */  
  171.     public Signature[] signatures;  
  172.       
  173.     /** 
  174.      * Application specified preferred configuration 
  175.      * {@link android.R.styleable#AndroidManifestUsesConfiguration 
  176.      * <uses-configuration>} tags included under <manifest>, 
  177.      * or null if there were none. This is only filled in if the flag 
  178.      * {@link PackageManager#GET_CONFIGURATIONS} was set.   
  179.      */  
  180.     public ConfigurationInfo[] configPreferences;  
  181.   
  182.     /** 
  183.      * The features that this application has said it requires. 
  184.      */  
  185.     public FeatureInfo[] reqFeatures;  
  186.   
  187.     /** 
  188.      * Constant corresponding to <code>auto</code> in 
  189.      * the {@link android.R.attr#installLocation} attribute. 
  190.      * @hide 
  191.      */  
  192.     public static final int INSTALL_LOCATION_UNSPECIFIED = -1;  
  193.     /** 
  194.      * Constant corresponding to <code>auto</code> in 
  195.      * the {@link android.R.attr#installLocation} attribute. 
  196.      * @hide 
  197.      */  
  198.     public static final int INSTALL_LOCATION_AUTO = 0;  
  199.     /** 
  200.      * Constant corresponding to <code>internalOnly</code> in 
  201.      * the {@link android.R.attr#installLocation} attribute. 
  202.      * @hide 
  203.      */  
  204.     public static final int INSTALL_LOCATION_INTERNAL_ONLY = 1;  
  205.     /** 
  206.      * Constant corresponding to <code>preferExternal</code> in 
  207.      * the {@link android.R.attr#installLocation} attribute. 
  208.      * @hide 
  209.      */  
  210.     public static final int INSTALL_LOCATION_PREFER_EXTERNAL = 2;  
  211.     /** 
  212.      * The install location requested by the activity.  From the 
  213.      * {@link android.R.attr#installLocation} attribute, one of 
  214.      * {@link #INSTALL_LOCATION_AUTO}, 
  215.      * {@link #INSTALL_LOCATION_INTERNAL_ONLY}, 
  216.      * {@link #INSTALL_LOCATION_PREFER_EXTERNAL} 
  217.      * @hide 
  218.      */  
  219.     public int installLocation = INSTALL_LOCATION_INTERNAL_ONLY;  
  220.       
  221.     public PackageInfo() {  
  222.     }  
  223.   
  224.     public String toString() {  
  225.         return "PackageInfo{"  
  226.             + Integer.toHexString(System.identityHashCode(this))  
  227.             + " " + packageName + "}";  
  228.     }  
  229.   
  230.     public int describeContents() {  
  231.         return 0;  
  232.     }  
  233.   
  234.     public void writeToParcel(Parcel dest, int parcelableFlags) {  
  235.         dest.writeString(packageName);  
  236.         dest.writeInt(versionCode);  
  237.         dest.writeString(versionName);  
  238.         dest.writeString(sharedUserId);  
  239.         dest.writeInt(sharedUserLabel);  
  240.         if (applicationInfo != null) {  
  241.             dest.writeInt(1);  
  242.             applicationInfo.writeToParcel(dest, parcelableFlags);  
  243.         } else {  
  244.             dest.writeInt(0);  
  245.         }  
  246.         dest.writeLong(firstInstallTime);  
  247.         dest.writeLong(lastUpdateTime);  
  248.         dest.writeIntArray(gids);  
  249.         dest.writeTypedArray(activities, parcelableFlags);  
  250.         dest.writeTypedArray(receivers, parcelableFlags);  
  251.         dest.writeTypedArray(services, parcelableFlags);  
  252.         dest.writeTypedArray(providers, parcelableFlags);  
  253.         dest.writeTypedArray(instrumentation, parcelableFlags);  
  254.         dest.writeTypedArray(permissions, parcelableFlags);  
  255.         dest.writeStringArray(requestedPermissions);  
  256.         dest.writeIntArray(requestedPermissionsFlags);  
  257.         dest.writeTypedArray(signatures, parcelableFlags);  
  258.         dest.writeTypedArray(configPreferences, parcelableFlags);  
  259.         dest.writeTypedArray(reqFeatures, parcelableFlags);  
  260.         dest.writeInt(installLocation);  
  261.     }  
  262.   
  263.     public static final Parcelable.Creator<PackageInfo> CREATOR  
  264.             = new Parcelable.Creator<PackageInfo>() {  
  265.         public PackageInfo createFromParcel(Parcel source) {  
  266.             return new PackageInfo(source);  
  267.         }  
  268.   
  269.         public PackageInfo[] newArray(int size) {  
  270.             return new PackageInfo[size];  
  271.         }  
  272.     };  
  273.   
  274.     private PackageInfo(Parcel source) {  
  275.        <span style="color:#3333ff;"> packageName = source.readString();  
  276.         versionCode = source.readInt();  
  277.         versionName = source.readString();</span>  
  278.         sharedUserId = source.readString();  
  279.         sharedUserLabel = source.readInt();  
  280.         int hasApp = source.readInt();  
  281.         if (hasApp != 0) {  
  282.             applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source);  
  283.         }  
  284.         <span style="color:#333399;">firstInstallTime = source.readLong();  
  285.         lastUpdateTime = source.readLong();</span>  
  286.         gids = source.createIntArray();  
  287.         activities = source.createTypedArray(ActivityInfo.CREATOR);  
  288.         receivers = source.createTypedArray(ActivityInfo.CREATOR);  
  289.         services = source.createTypedArray(ServiceInfo.CREATOR);  
  290.         providers = source.createTypedArray(ProviderInfo.CREATOR);  
  291.         instrumentation = source.createTypedArray(InstrumentationInfo.CREATOR);  
  292.         permissions = source.createTypedArray(PermissionInfo.CREATOR);  
  293.         requestedPermissions = source.createStringArray();  
  294.         requestedPermissionsFlags = source.createIntArray();  
  295.         signatures = source.createTypedArray(Signature.CREATOR);  
  296.         configPreferences = source.createTypedArray(ConfigurationInfo.CREATOR);  
  297.         reqFeatures = source.createTypedArray(FeatureInfo.CREATOR);  
  298.         installLocation = source.readInt();  
  299.     }  
  300. }  

ApplicationInfo.java源码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package android.content.pm;  
  18.   
  19. import android.content.pm.PackageManager.NameNotFoundException;  
  20. import android.content.res.Resources;  
  21. import android.graphics.drawable.Drawable;  
  22. import android.os.Parcel;  
  23. import android.os.Parcelable;  
  24. import android.util.Printer;  
  25.   
  26. import java.text.Collator;  
  27. import java.util.Comparator;  
  28.   
  29. /** 
  30.  * Information you can retrieve about a particular application.  This 
  31.  * corresponds to information collected from the AndroidManifest.xml's 
  32.  * <application> tag. 
  33.  */  
  34. public class ApplicationInfo extends PackageItemInfo implements Parcelable {  
  35.       
  36.     /** 
  37.      * Default task affinity of all activities in this application. See  
  38.      * {@link ActivityInfo#taskAffinity} for more information.  This comes  
  39.      * from the "taskAffinity" attribute.  
  40.      */  
  41.     public String taskAffinity;  
  42.       
  43.     /** 
  44.      * Optional name of a permission required to be able to access this 
  45.      * application's components.  From the "permission" attribute. 
  46.      */  
  47.     public String permission;  
  48.       
  49.     /** 
  50.      * The name of the process this application should run in.  From the 
  51.      * "process" attribute or, if not set, the same as 
  52.      * <var>packageName</var>. 
  53.      */  
  54.     public String processName;  
  55.       
  56.     /** 
  57.      * Class implementing the Application object.  From the "class" 
  58.      * attribute. 
  59.      */  
  60.     public String className;  
  61.       
  62.     /** 
  63.      * A style resource identifier (in the package's resources) of the 
  64.      * description of an application.  From the "description" attribute 
  65.      * or, if not set, 0. 
  66.      */  
  67.     public int descriptionRes;      
  68.       
  69.     /** 
  70.      * A style resource identifier (in the package's resources) of the 
  71.      * default visual theme of the application.  From the "theme" attribute 
  72.      * or, if not set, 0. 
  73.      */  
  74.     public int theme;  
  75.       
  76.     /** 
  77.      * Class implementing the Application's manage space 
  78.      * functionality.  From the "manageSpaceActivity" 
  79.      * attribute. This is an optional attribute and will be null if 
  80.      * applications don't specify it in their manifest 
  81.      */  
  82.     public String manageSpaceActivityName;      
  83.       
  84.     /** 
  85.      * Class implementing the Application's backup functionality.  From 
  86.      * the "backupAgent" attribute.  This is an optional attribute and 
  87.      * will be null if the application does not specify it in its manifest. 
  88.      *  
  89.      * <p>If android:allowBackup is set to false, this attribute is ignored. 
  90.      */  
  91.     public String backupAgentName;  
  92.   
  93.     /** 
  94.      * The default extra UI options for activities in this application. 
  95.      * Set from the {@link android.R.attr#uiOptions} attribute in the 
  96.      * activity's manifest. 
  97.      */  
  98.     public int uiOptions = 0;  
  99.   
  100.     /** 
  101.      * Value for {@link #flags}: if set, this application is installed in the 
  102.      * device's system image. 
  103.      */  
  104.     public static final int FLAG_SYSTEM = 1<<0;  
  105.       
  106.     /** 
  107.      * Value for {@link #flags}: set to true if this application would like to 
  108.      * allow debugging of its 
  109.      * code, even when installed on a non-development system.  Comes 
  110.      * from {@link android.R.styleable#AndroidManifestApplication_debuggable 
  111.      * android:debuggable} of the <application> tag. 
  112.      */  
  113.     public static final int FLAG_DEBUGGABLE = 1<<1;  
  114.       
  115.     /** 
  116.      * Value for {@link #flags}: set to true if this application has code 
  117.      * associated with it.  Comes 
  118.      * from {@link android.R.styleable#AndroidManifestApplication_hasCode 
  119.      * android:hasCode} of the <application> tag. 
  120.      */  
  121.     public static final int FLAG_HAS_CODE = 1<<2;  
  122.       
  123.     /** 
  124.      * Value for {@link #flags}: set to true if this application is persistent. 
  125.      * Comes from {@link android.R.styleable#AndroidManifestApplication_persistent 
  126.      * android:persistent} of the <application> tag. 
  127.      */  
  128.     public static final int FLAG_PERSISTENT = 1<<3;  
  129.   
  130.     /** 
  131.      * Value for {@link #flags}: set to true if this application holds the 
  132.      * {@link android.Manifest.permission#FACTORY_TEST} permission and the 
  133.      * device is running in factory test mode. 
  134.      */  
  135.     public static final int FLAG_FACTORY_TEST = 1<<4;  
  136.   
  137.     /** 
  138.      * Value for {@link #flags}: default value for the corresponding ActivityInfo flag. 
  139.      * Comes from {@link android.R.styleable#AndroidManifestApplication_allowTaskReparenting 
  140.      * android:allowTaskReparenting} of the <application> tag. 
  141.      */  
  142.     public static final int FLAG_ALLOW_TASK_REPARENTING = 1<<5;  
  143.       
  144.     /** 
  145.      * Value for {@link #flags}: default value for the corresponding ActivityInfo flag. 
  146.      * Comes from {@link android.R.styleable#AndroidManifestApplication_allowClearUserData 
  147.      * android:allowClearUserData} of the <application> tag. 
  148.      */  
  149.     public static final int FLAG_ALLOW_CLEAR_USER_DATA = 1<<6;  
  150.       
  151.     /** 
  152.      * Value for {@link #flags}: this is set if this application has been 
  153.      * install as an update to a built-in system application. 
  154.      */  
  155.     public static final int FLAG_UPDATED_SYSTEM_APP = 1<<7;  
  156.       
  157.     /** 
  158.      * Value for {@link #flags}: this is set of the application has specified 
  159.      * {@link android.R.styleable#AndroidManifestApplication_testOnly 
  160.      * android:testOnly} to be true. 
  161.      */  
  162.     public static final int FLAG_TEST_ONLY = 1<<8;  
  163.   
  164.     /** 
  165.      * Value for {@link #flags}: true when the application's window can be 
  166.      * reduced in size for smaller screens.  Corresponds to 
  167.      * {@link android.R.styleable#AndroidManifestSupportsScreens_smallScreens 
  168.      * android:smallScreens}. 
  169.      */  
  170.     public static final int FLAG_SUPPORTS_SMALL_SCREENS = 1<<9;  
  171.       
  172.     /** 
  173.      * Value for {@link #flags}: true when the application's window can be 
  174.      * displayed on normal screens.  Corresponds to 
  175.      * {@link android.R.styleable#AndroidManifestSupportsScreens_normalScreens 
  176.      * android:normalScreens}. 
  177.      */  
  178.     public static final int FLAG_SUPPORTS_NORMAL_SCREENS = 1<<10;   
  179.       
  180.     /** 
  181.      * Value for {@link #flags}: true when the application's window can be 
  182.      * increased in size for larger screens.  Corresponds to 
  183.      * {@link android.R.styleable#AndroidManifestSupportsScreens_largeScreens 
  184.      * android:largeScreens}. 
  185.      */  
  186.     public static final int FLAG_SUPPORTS_LARGE_SCREENS = 1<<11;  
  187.       
  188.     /** 
  189.      * Value for {@link #flags}: true when the application knows how to adjust 
  190.      * its UI for different screen sizes.  Corresponds to 
  191.      * {@link android.R.styleable#AndroidManifestSupportsScreens_resizeable 
  192.      * android:resizeable}. 
  193.      */  
  194.     public static final int FLAG_RESIZEABLE_FOR_SCREENS = 1<<12;  
  195.       
  196.     /** 
  197.      * Value for {@link #flags}: true when the application knows how to 
  198.      * accomodate different screen densities.  Corresponds to 
  199.      * {@link android.R.styleable#AndroidManifestSupportsScreens_anyDensity 
  200.      * android:anyDensity}. 
  201.      */  
  202.     public static final int FLAG_SUPPORTS_SCREEN_DENSITIES = 1<<13;  
  203.       
  204.     /** 
  205.      * Value for {@link #flags}: set to true if this application would like to 
  206.      * request the VM to operate under the safe mode. Comes from 
  207.      * {@link android.R.styleable#AndroidManifestApplication_vmSafeMode 
  208.      * android:vmSafeMode} of the <application> tag. 
  209.      */  
  210.     public static final int FLAG_VM_SAFE_MODE = 1<<14;  
  211.   
  212.     /** 
  213.      * Value for {@link #flags}: set to <code>false</code> if the application does not wish 
  214.      * to permit any OS-driven backups of its data; <code>true</code> otherwise. 
  215.      *  
  216.      * <p>Comes from the 
  217.      * {@link android.R.styleable#AndroidManifestApplication_allowBackup android:allowBackup} 
  218.      * attribute of the <application> tag. 
  219.      */  
  220.     public static final int FLAG_ALLOW_BACKUP = 1<<15;  
  221.   
  222.     /** 
  223.      * Value for {@link #flags}: set to <code>false</code> if the application must be kept 
  224.      * in memory following a full-system restore operation; <code>true</code> otherwise. 
  225.      * Ordinarily, during a full system restore operation each application is shut down 
  226.      * following execution of its agent's onRestore() method.  Setting this attribute to 
  227.      * <code>false</code> prevents this.  Most applications will not need to set this attribute. 
  228.      * 
  229.      * <p>If 
  230.      * {@link android.R.styleable#AndroidManifestApplication_allowBackup android:allowBackup} 
  231.      * is set to <code>false</code> or no 
  232.      * {@link android.R.styleable#AndroidManifestApplication_backupAgent android:backupAgent} 
  233.      * is specified, this flag will be ignored. 
  234.      * 
  235.      * <p>Comes from the 
  236.      * {@link android.R.styleable#AndroidManifestApplication_killAfterRestore android:killAfterRestore} 
  237.      * attribute of the <application> tag. 
  238.      */  
  239.     public static final int FLAG_KILL_AFTER_RESTORE = 1<<16;  
  240.   
  241.     /** 
  242.      * Value for {@link #flags}: Set to <code>true</code> if the application's backup 
  243.      * agent claims to be able to handle restore data even "from the future," 
  244.      * i.e. from versions of the application with a versionCode greater than 
  245.      * the one currently installed on the device.  <i>Use with caution!</i>  By default 
  246.      * this attribute is <code>false</code> and the Backup Manager will ensure that data 
  247.      * from "future" versions of the application are never supplied during a restore operation. 
  248.      * 
  249.      * <p>If 
  250.      * {@link android.R.styleable#AndroidManifestApplication_allowBackup android:allowBackup} 
  251.      * is set to <code>false</code> or no 
  252.      * {@link android.R.styleable#AndroidManifestApplication_backupAgent android:backupAgent} 
  253.      * is specified, this flag will be ignored. 
  254.      * 
  255.      * <p>Comes from the 
  256.      * {@link android.R.styleable#AndroidManifestApplication_restoreAnyVersion android:restoreAnyVersion} 
  257.      * attribute of the <application> tag. 
  258.      */  
  259.     public static final int FLAG_RESTORE_ANY_VERSION = 1<<17;  
  260.   
  261.     /** 
  262.      * Value for {@link #flags}: Set to true if the application is 
  263.      * currently installed on external/removable/unprotected storage.  Such 
  264.      * applications may not be available if their storage is not currently 
  265.      * mounted.  When the storage it is on is not available, it will look like 
  266.      * the application has been uninstalled (its .apk is no longer available) 
  267.      * but its persistent data is not removed. 
  268.      */  
  269.     public static final int FLAG_EXTERNAL_STORAGE = 1<<18;  
  270.   
  271.     /** 
  272.      * Value for {@link #flags}: true when the application's window can be 
  273.      * increased in size for extra large screens.  Corresponds to 
  274.      * {@link android.R.styleable#AndroidManifestSupportsScreens_xlargeScreens 
  275.      * android:xlargeScreens}. 
  276.      */  
  277.     public static final int FLAG_SUPPORTS_XLARGE_SCREENS = 1<<19;  
  278.       
  279.     /** 
  280.      * Value for {@link #flags}: true when the application has requested a 
  281.      * large heap for its processes.  Corresponds to 
  282.      * {@link android.R.styleable#AndroidManifestApplication_largeHeap 
  283.      * android:largeHeap}. 
  284.      */  
  285.     public static final int FLAG_LARGE_HEAP = 1<<20;  
  286.   
  287.     /** 
  288.      * Value for {@link #flags}: true if this application's package is in 
  289.      * the stopped state. 
  290.      */  
  291.     public static final int FLAG_STOPPED = 1<<21;  
  292.   
  293.     /** 
  294.      * Value for {@link #flags}: true  when the application is willing to support 
  295.      * RTL (right to left). All activities will inherit this value. 
  296.      * 
  297.      * Set from the {@link android.R.attr#supportsRtl} attribute in the 
  298.      * activity's manifest. 
  299.      * 
  300.      * Default value is false (no support for RTL). 
  301.      */  
  302.     public static final int FLAG_SUPPORTS_RTL = 1<<22;  
  303.   
  304.     /** 
  305.      * Value for {@link #flags}: true if the application is currently 
  306.      * installed for the calling user. 
  307.      */  
  308.     public static final int FLAG_INSTALLED = 1<<23;  
  309.   
  310.     /** 
  311.      * Value for {@link #flags}: true if the application only has its 
  312.      * data installed; the application package itself does not currently 
  313.      * exist on the device. 
  314.      */  
  315.     public static final int FLAG_IS_DATA_ONLY = 1<<24;  
  316.   
  317.     /** 
  318.      * Value for {@link #flags}: Set to true if the application has been 
  319.      * installed using the forward lock option. 
  320.      * 
  321.      * NOTE: DO NOT CHANGE THIS VALUE!  It is saved in packages.xml. 
  322.      *  
  323.      * {@hide} 
  324.      */  
  325.     public static final int FLAG_FORWARD_LOCK = 1<<29;  
  326.   
  327.     /** 
  328.      * Value for {@link #flags}: set to <code>true</code> if the application 
  329.      * has reported that it is heavy-weight, and thus can not participate in 
  330.      * the normal application lifecycle. 
  331.      * 
  332.      * <p>Comes from the 
  333.      * {@link android.R.styleable#AndroidManifestApplication_cantSaveState android:cantSaveState} 
  334.      * attribute of the <application> tag. 
  335.      * 
  336.      * {@hide} 
  337.      */  
  338.     public static final int FLAG_CANT_SAVE_STATE = 1<<28;  
  339.   
  340.     /** 
  341.      * Flags associated with the application.  Any combination of 
  342.      * {@link #FLAG_SYSTEM}, {@link #FLAG_DEBUGGABLE}, {@link #FLAG_HAS_CODE}, 
  343.      * {@link #FLAG_PERSISTENT}, {@link #FLAG_FACTORY_TEST}, and 
  344.      * {@link #FLAG_ALLOW_TASK_REPARENTING} 
  345.      * {@link #FLAG_ALLOW_CLEAR_USER_DATA}, {@link #FLAG_UPDATED_SYSTEM_APP}, 
  346.      * {@link #FLAG_TEST_ONLY}, {@link #FLAG_SUPPORTS_SMALL_SCREENS}, 
  347.      * {@link #FLAG_SUPPORTS_NORMAL_SCREENS}, 
  348.      * {@link #FLAG_SUPPORTS_LARGE_SCREENS}, {@link #FLAG_SUPPORTS_XLARGE_SCREENS}, 
  349.      * {@link #FLAG_RESIZEABLE_FOR_SCREENS}, 
  350.      * {@link #FLAG_SUPPORTS_SCREEN_DENSITIES}, {@link #FLAG_VM_SAFE_MODE}, 
  351.      * {@link #FLAG_INSTALLED}. 
  352.      */  
  353.     public int flags = 0;  
  354.       
  355.     /** 
  356.      * The required smallest screen width the application can run on.  If 0, 
  357.      * nothing has been specified.  Comes from 
  358.      * {@link android.R.styleable#AndroidManifestSupportsScreens_requiresSmallestWidthDp 
  359.      * android:requiresSmallestWidthDp} attribute of the <supports-screens> tag. 
  360.      */  
  361.     public int requiresSmallestWidthDp = 0;  
  362.   
  363.     /** 
  364.      * The maximum smallest screen width the application is designed for.  If 0, 
  365.      * nothing has been specified.  Comes from 
  366.      * {@link android.R.styleable#AndroidManifestSupportsScreens_compatibleWidthLimitDp 
  367.      * android:compatibleWidthLimitDp} attribute of the <supports-screens> tag. 
  368.      */  
  369.     public int compatibleWidthLimitDp = 0;  
  370.   
  371.     /** 
  372.      * The maximum smallest screen width the application will work on.  If 0, 
  373.      * nothing has been specified.  Comes from 
  374.      * {@link android.R.styleable#AndroidManifestSupportsScreens_largestWidthLimitDp 
  375.      * android:largestWidthLimitDp} attribute of the <supports-screens> tag. 
  376.      */  
  377.     public int largestWidthLimitDp = 0;  
  378.   
  379.     /** 
  380.      * Full path to the location of this package. 
  381.      */  
  382.     public String sourceDir;  
  383.   
  384.     /** 
  385.      * Full path to the location of the publicly available parts of this 
  386.      * package (i.e. the primary resource package and manifest).  For 
  387.      * non-forward-locked apps this will be the same as {@link #sourceDir). 
  388.      */  
  389.     public String publicSourceDir;  
  390.       
  391.     /** 
  392.      * Full paths to the locations of extra resource packages this application 
  393.      * uses. This field is only used if there are extra resource packages, 
  394.      * otherwise it is null. 
  395.      *  
  396.      * {@hide} 
  397.      */  
  398.     public String[] resourceDirs;  
  399.   
  400.     /** 
  401.      * Paths to all shared libraries this application is linked against.  This 
  402.      * field is only set if the {@link PackageManager#GET_SHARED_LIBRARY_FILES 
  403.      * PackageManager.GET_SHARED_LIBRARY_FILES} flag was used when retrieving 
  404.      * the structure. 
  405.      */  
  406.     public String[] sharedLibraryFiles;  
  407.       
  408.     /** 
  409.      * Full path to a directory assigned to the package for its persistent 
  410.      * data. 
  411.      */  
  412.     public String dataDir;  
  413.   
  414.     /** 
  415.      * Full path to the directory where native JNI libraries are stored. 
  416.      */  
  417.     public String nativeLibraryDir;  
  418.   
  419.     /** 
  420.      * The kernel user-ID that has been assigned to this application; 
  421.      * currently this is not a unique ID (multiple applications can have 
  422.      * the same uid). 
  423.      */  
  424.     public int uid;  
  425.       
  426.     /** 
  427.      * The minimum SDK version this application targets.  It may run on earlier 
  428.      * versions, but it knows how to work with any new behavior added at this 
  429.      * version.  Will be {@link android.os.Build.VERSION_CODES#CUR_DEVELOPMENT} 
  430.      * if this is a development build and the app is targeting that.  You should 
  431.      * compare that this number is >= the SDK version number at which your 
  432.      * behavior was introduced. 
  433.      */  
  434.     public int targetSdkVersion;  
  435.       
  436.     /** 
  437.      * When false, indicates that all components within this application are 
  438.      * considered disabled, regardless of their individually set enabled status. 
  439.      */  
  440.     public boolean enabled = true;  
  441.   
  442.     /** 
  443.      * For convenient access to the current enabled setting of this app. 
  444.      * @hide 
  445.      */  
  446.     public int enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;  
  447.   
  448.     /** 
  449.      * For convenient access to package's install location. 
  450.      * @hide 
  451.      */  
  452.     public int installLocation = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;  
  453.       
  454.     public void dump(Printer pw, String prefix) {  
  455.         super.dumpFront(pw, prefix);  
  456.         if (className != null) {  
  457.             pw.println(prefix + "className=" + className);  
  458.         }  
  459.         if (permission != null) {  
  460.             pw.println(prefix + "permission=" + permission);  
  461.         }  
  462.         pw.println(prefix + "processName=" + processName);  
  463.         pw.println(prefix + "taskAffinity=" + taskAffinity);  
  464.         pw.println(prefix + "uid=" + uid + " flags=0x" + Integer.toHexString(flags)  
  465.                 + " theme=0x" + Integer.toHexString(theme));  
  466.         pw.println(prefix + "requiresSmallestWidthDp=" + requiresSmallestWidthDp  
  467.                 + " compatibleWidthLimitDp=" + compatibleWidthLimitDp  
  468.                 + " largestWidthLimitDp=" + largestWidthLimitDp);  
  469.         pw.println(prefix + "sourceDir=" + sourceDir);  
  470.         if (sourceDir == null) {  
  471.             if (publicSourceDir != null) {  
  472.                 pw.println(prefix + "publicSourceDir=" + publicSourceDir);  
  473.             }  
  474.         } else if (!sourceDir.equals(publicSourceDir)) {  
  475.             pw.println(prefix + "publicSourceDir=" + publicSourceDir);  
  476.         }  
  477.         if (resourceDirs != null) {  
  478.             pw.println(prefix + "resourceDirs=" + resourceDirs);  
  479.         }  
  480.         pw.println(prefix + "dataDir=" + dataDir);  
  481.         if (sharedLibraryFiles != null) {  
  482.             pw.println(prefix + "sharedLibraryFiles=" + sharedLibraryFiles);  
  483.         }  
  484.         pw.println(prefix + "enabled=" + enabled + " targetSdkVersion=" + targetSdkVersion);  
  485.         if (manageSpaceActivityName != null) {  
  486.             pw.println(prefix + "manageSpaceActivityName="+manageSpaceActivityName);  
  487.         }  
  488.         if (descriptionRes != 0) {  
  489.             pw.println(prefix + "description=0x"+Integer.toHexString(descriptionRes));  
  490.         }  
  491.         if (uiOptions != 0) {  
  492.             pw.println(prefix + "uiOptions=0x" + Integer.toHexString(uiOptions));  
  493.         }  
  494.         pw.println(prefix + "supportsRtl=" + (hasRtlSupport() ? "true" : "false"));  
  495.         super.dumpBack(pw, prefix);  
  496.     }  
  497.   
  498.     /** 
  499.      * @return true if "supportsRtl" has been set to true in the AndroidManifest 
  500.      * @hide 
  501.      */  
  502.     public boolean hasRtlSupport() {  
  503.         return (flags & FLAG_SUPPORTS_RTL) == FLAG_SUPPORTS_RTL;  
  504.     }  
  505.       
  506.     public static class DisplayNameComparator  
  507.             implements Comparator<ApplicationInfo> {  
  508.         public DisplayNameComparator(PackageManager pm) {  
  509.             mPM = pm;  
  510.         }  
  511.   
  512.         public final int compare(ApplicationInfo aa, ApplicationInfo ab) {  
  513.             CharSequence  sa = mPM.getApplicationLabel(aa);  
  514.             if (sa == null) {  
  515.                 sa = aa.packageName;  
  516.             }  
  517.             CharSequence  sb = mPM.getApplicationLabel(ab);  
  518.             if (sb == null) {  
  519.                 sb = ab.packageName;  
  520.             }  
  521.               
  522.             return sCollator.compare(sa.toString(), sb.toString());  
  523.         }  
  524.   
  525.         private final Collator   sCollator = Collator.getInstance();  
  526.         private PackageManager   mPM;  
  527.     }  
  528.   
  529.     public ApplicationInfo() {  
  530.     }  
  531.       
  532.     public ApplicationInfo(ApplicationInfo orig) {  
  533.         super(orig);  
  534.         taskAffinity = orig.taskAffinity;  
  535.         permission = orig.permission;  
  536.         processName = orig.processName;  
  537.         className = orig.className;  
  538.         theme = orig.theme;  
  539.         flags = orig.flags;  
  540.         requiresSmallestWidthDp = orig.requiresSmallestWidthDp;  
  541.         compatibleWidthLimitDp = orig.compatibleWidthLimitDp;  
  542.         largestWidthLimitDp = orig.largestWidthLimitDp;  
  543.         sourceDir = orig.sourceDir;  
  544.         publicSourceDir = orig.publicSourceDir;  
  545.         nativeLibraryDir = orig.nativeLibraryDir;  
  546.         resourceDirs = orig.resourceDirs;  
  547.         sharedLibraryFiles = orig.sharedLibraryFiles;  
  548.         dataDir = orig.dataDir;  
  549.         uid = orig.uid;  
  550.         targetSdkVersion = orig.targetSdkVersion;  
  551.         enabled = orig.enabled;  
  552.         enabledSetting = orig.enabledSetting;  
  553.         installLocation = orig.installLocation;  
  554.         manageSpaceActivityName = orig.manageSpaceActivityName;  
  555.         descriptionRes = orig.descriptionRes;  
  556.         uiOptions = orig.uiOptions;  
  557.         backupAgentName = orig.backupAgentName;  
  558.     }  
  559.   
  560.   
  561.     public String toString() {  
  562.         return "ApplicationInfo{"  
  563.             + Integer.toHexString(System.identityHashCode(this))  
  564.             + " " + packageName + "}";  
  565.     }  
  566.   
  567.     public int describeContents() {  
  568.         return 0;  
  569.     }  
  570.   
  571.     public void writeToParcel(Parcel dest, int parcelableFlags) {  
  572.         super.writeToParcel(dest, parcelableFlags);  
  573.         dest.writeString(taskAffinity);  
  574.         dest.writeString(permission);  
  575.         dest.writeString(processName);  
  576.         dest.writeString(className);  
  577.         dest.writeInt(theme);  
  578.         dest.writeInt(flags);  
  579.         dest.writeInt(requiresSmallestWidthDp);  
  580.         dest.writeInt(compatibleWidthLimitDp);  
  581.         dest.writeInt(largestWidthLimitDp);  
  582.         dest.writeString(sourceDir);  
  583.         dest.writeString(publicSourceDir);  
  584.         dest.writeString(nativeLibraryDir);  
  585.         dest.writeStringArray(resourceDirs);  
  586.         dest.writeStringArray(sharedLibraryFiles);  
  587.         dest.writeString(dataDir);  
  588.         dest.writeInt(uid);  
  589.         dest.writeInt(targetSdkVersion);  
  590.         dest.writeInt(enabled ? 1 : 0);  
  591.         dest.writeInt(enabledSetting);  
  592.         dest.writeInt(installLocation);  
  593.         dest.writeString(manageSpaceActivityName);  
  594.         dest.writeString(backupAgentName);  
  595.         dest.writeInt(descriptionRes);  
  596.         dest.writeInt(uiOptions);  
  597.     }  
  598.   
  599.     public static final Parcelable.Creator<ApplicationInfo> CREATOR  
  600.             = new Parcelable.Creator<ApplicationInfo>() {  
  601.         public ApplicationInfo createFromParcel(Parcel source) {  
  602.             return new ApplicationInfo(source);  
  603.         }  
  604.         public ApplicationInfo[] newArray(int size) {  
  605.             return new ApplicationInfo[size];  
  606.         }  
  607.     };  
  608.   
  609.     private ApplicationInfo(Parcel source) {  
  610.         super(source);  
  611.         taskAffinity = source.readString();  
  612.         permission = source.readString();  
  613.         processName = source.readString();  
  614.         className = source.readString();  
  615.         theme = source.readInt();  
  616.         flags = source.readInt();  
  617.         requiresSmallestWidthDp = source.readInt();  
  618.         compatibleWidthLimitDp = source.readInt();  
  619.         largestWidthLimitDp = source.readInt();  
  620.         sourceDir = source.readString();  
  621.         publicSourceDir = source.readString();  
  622.         nativeLibraryDir = source.readString();  
  623.         resourceDirs = source.readStringArray();  
  624.         sharedLibraryFiles = source.readStringArray();  
  625.         dataDir = source.readString();  
  626.         uid = source.readInt();  
  627.         targetSdkVersion = source.readInt();  
  628.         enabled = source.readInt() != 0;  
  629.         enabledSetting = source.readInt();  
  630.         installLocation = source.readInt();  
  631.         manageSpaceActivityName = source.readString();  
  632.         backupAgentName = source.readString();  
  633.         descriptionRes = source.readInt();  
  634.         uiOptions = source.readInt();  
  635.     }  
  636.   
  637.     /** 
  638.      * Retrieve the textual description of the application.  This 
  639.      * will call back on the given PackageManager to load the description from 
  640.      * the application. 
  641.      * 
  642.      * @param pm A PackageManager from which the label can be loaded; usually 
  643.      * the PackageManager from which you originally retrieved this item. 
  644.      * 
  645.      * @return Returns a CharSequence containing the application's description. 
  646.      * If there is no description, null is returned. 
  647.      */  
  648.     public CharSequence loadDescription(PackageManager pm) {  
  649.         if (descriptionRes != 0) {  
  650.             CharSequence label = pm.getText(packageName, descriptionRes, this);  
  651.             if (label != null) {  
  652.                 return label;  
  653.             }  
  654.         }  
  655.         return null;  
  656.     }  
  657.   
  658.     /** 
  659.      * Disable compatibility mode 
  660.      *  
  661.      * @hide 
  662.      */  
  663.     public void disableCompatibilityMode() {  
  664.         flags |= (FLAG_SUPPORTS_LARGE_SCREENS | FLAG_SUPPORTS_NORMAL_SCREENS |  
  665.                 FLAG_SUPPORTS_SMALL_SCREENS | FLAG_RESIZEABLE_FOR_SCREENS |  
  666.                 FLAG_SUPPORTS_SCREEN_DENSITIES | FLAG_SUPPORTS_XLARGE_SCREENS);  
  667.     }  
  668.       
  669.     /** 
  670.      * @hide 
  671.      */  
  672.     @Override protected Drawable loadDefaultIcon(PackageManager pm) {  
  673.         if ((flags & FLAG_EXTERNAL_STORAGE) != 0  
  674.                 && isPackageUnavailable(pm)) {  
  675.             return Resources.getSystem().getDrawable(  
  676.                     com.android.internal.R.drawable.sym_app_on_sd_unavailable_icon);  
  677.         }  
  678.         return pm.getDefaultActivityIcon();  
  679.     }  
  680.       
  681.     private boolean isPackageUnavailable(PackageManager pm) {  
  682.         try {  
  683.             return pm.getPackageInfo(packageName, 0) == null;  
  684.         } catch (NameNotFoundException ex) {  
  685.             return true;  
  686.         }  
  687.     }  
  688.       
  689.     /** 
  690.      * @hide 
  691.      */  
  692.     @Override protected ApplicationInfo getApplicationInfo() {  
  693.         return this;  
  694.     }  
  695. }  

PackageItemInfo.java源码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Copyright (C) 2007 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package android.content.pm;  
  18.   
  19. import android.content.res.XmlResourceParser;  
  20.   
  21. import android.graphics.drawable.Drawable;  
  22. import android.os.Bundle;  
  23. import android.os.Parcel;  
  24. import android.text.TextUtils;  
  25. import android.util.Printer;  
  26.   
  27. import java.text.Collator;  
  28. import java.util.Comparator;  
  29.   
  30. /** 
  31.  * Base class containing information common to all package items held by 
  32.  * the package manager.  This provides a very common basic set of attributes: 
  33.  * a label, icon, and meta-data.  This class is not intended 
  34.  * to be used by itself; it is simply here to share common definitions 
  35.  * between all items returned by the package manager.  As such, it does not 
  36.  * itself implement Parcelable, but does provide convenience methods to assist 
  37.  * in the implementation of Parcelable in subclasses. 
  38.  */  
  39. public class PackageItemInfo {  
  40.     /** 
  41.      * Public name of this item. From the "android:name" attribute. 
  42.      */  
  43.     public String name;  
  44.       
  45.     /** 
  46.      * Name of the package that this item is in. 
  47.      */  
  48.     public String packageName;  
  49.       
  50.     /** 
  51.      * A string resource identifier (in the package's resources) of this 
  52.      * component's label.  From the "label" attribute or, if not set, 0. 
  53.      */  
  54.     public int labelRes;  
  55.       
  56.     /** 
  57.      * The string provided in the AndroidManifest file, if any.  You 
  58.      * probably don't want to use this.  You probably want 
  59.      * {@link PackageManager#getApplicationLabel} 
  60.      */  
  61.     public CharSequence nonLocalizedLabel;  
  62.       
  63.     /** 
  64.      * A drawable resource identifier (in the package's resources) of this 
  65.      * component's icon.  From the "icon" attribute or, if not set, 0. 
  66.      */  
  67.     public int icon;  
  68.       
  69.     /** 
  70.      * A drawable resource identifier (in the package's resources) of this 
  71.      * component's logo. Logos may be larger/wider than icons and are 
  72.      * displayed by certain UI elements in place of a name or name/icon 
  73.      * combination. From the "logo" attribute or, if not set, 0.  
  74.      */  
  75.     public int logo;  
  76.       
  77.     /** 
  78.      * Additional meta-data associated with this component.  This field 
  79.      * will only be filled in if you set the 
  80.      * {@link PackageManager#GET_META_DATA} flag when requesting the info. 
  81.      */  
  82.     public Bundle metaData;  
  83.       
  84.     public PackageItemInfo() {  
  85.     }  
  86.   
  87.     public PackageItemInfo(PackageItemInfo orig) {  
  88.         name = orig.name;  
  89.         if (name != null) name = name.trim();  
  90.         packageName = orig.packageName;  
  91.         labelRes = orig.labelRes;  
  92.         nonLocalizedLabel = orig.nonLocalizedLabel;  
  93.         if (nonLocalizedLabel != null) nonLocalizedLabel = nonLocalizedLabel.toString().trim();  
  94.         icon = orig.icon;  
  95.         logo = orig.logo;  
  96.         metaData = orig.metaData;  
  97.     }  
  98.   
  99.     /** 
  100.      * Retrieve the current textual label associated with this item.  This 
  101.      * will call back on the given PackageManager to load the label from 
  102.      * the application. 
  103.      *  
  104.      * @param pm A PackageManager from which the label can be loaded; usually 
  105.      * the PackageManager from which you originally retrieved this item. 
  106.      *  
  107.      * @return Returns a CharSequence containing the item's label.  If the 
  108.      * item does not have a label, its name is returned. 
  109.      */  
  110.     public CharSequence loadLabel(PackageManager pm) {  
  111.         if (nonLocalizedLabel != null) {  
  112.             return nonLocalizedLabel;  
  113.         }  
  114.         if (labelRes != 0) {  
  115.             CharSequence label = pm.getText(packageName, labelRes, getApplicationInfo());  
  116.             if (label != null) {  
  117.                 return label.toString().trim();  
  118.             }  
  119.         }  
  120.         if (name != null) {  
  121.             return name;  
  122.         }  
  123.         return packageName;  
  124.     }  
  125.       
  126.     /** 
  127.      * Retrieve the current graphical icon associated with this item.  This 
  128.      * will call back on the given PackageManager to load the icon from 
  129.      * the application. 
  130.      *  
  131.      * @param pm A PackageManager from which the icon can be loaded; usually 
  132.      * the PackageManager from which you originally retrieved this item. 
  133.      *  
  134.      * @return Returns a Drawable containing the item's icon.  If the 
  135.      * item does not have an icon, the item's default icon is returned 
  136.      * such as the default activity icon. 
  137.      */  
  138.     public Drawable loadIcon(PackageManager pm) {  
  139.         if (icon != 0) {  
  140.             Drawable dr = pm.getDrawable(packageName, icon, getApplicationInfo());  
  141.             if (dr != null) {  
  142.                 return dr;  
  143.             }  
  144.         }  
  145.         return loadDefaultIcon(pm);  
  146.     }  
  147.       
  148.     /** 
  149.      * Retrieve the default graphical icon associated with this item. 
  150.      *  
  151.      * @param pm A PackageManager from which the icon can be loaded; usually 
  152.      * the PackageManager from which you originally retrieved this item. 
  153.      *  
  154.      * @return Returns a Drawable containing the item's default icon 
  155.      * such as the default activity icon. 
  156.      *  
  157.      * @hide 
  158.      */  
  159.     protected Drawable loadDefaultIcon(PackageManager pm) {  
  160.         return pm.getDefaultActivityIcon();  
  161.     }  
  162.       
  163.     /** 
  164.      * Retrieve the current graphical logo associated with this item. This 
  165.      * will call back on the given PackageManager to load the logo from 
  166.      * the application. 
  167.      *  
  168.      * @param pm A PackageManager from which the logo can be loaded; usually 
  169.      * the PackageManager from which you originally retrieved this item. 
  170.      *  
  171.      * @return Returns a Drawable containing the item's logo. If the item 
  172.      * does not have a logo, this method will return null. 
  173.      */  
  174.     public Drawable loadLogo(PackageManager pm) {  
  175.         if (logo != 0) {  
  176.             Drawable d = pm.getDrawable(packageName, logo, getApplicationInfo());  
  177.             if (d != null) {  
  178.                 return d;  
  179.             }  
  180.         }  
  181.         return loadDefaultLogo(pm);  
  182.     }  
  183.       
  184.     /** 
  185.      * Retrieve the default graphical logo associated with this item. 
  186.      *  
  187.      * @param pm A PackageManager from which the logo can be loaded; usually 
  188.      * the PackageManager from which you originally retrieved this item. 
  189.      *  
  190.      * @return Returns a Drawable containing the item's default logo 
  191.      * or null if no default logo is available. 
  192.      *  
  193.      * @hide 
  194.      */  
  195.     protected Drawable loadDefaultLogo(PackageManager pm) {  
  196.         return null;  
  197.     }  
  198.       
  199.     /** 
  200.      * Load an XML resource attached to the meta-data of this item.  This will 
  201.      * retrieved the name meta-data entry, and if defined call back on the 
  202.      * given PackageManager to load its XML file from the application. 
  203.      *  
  204.      * @param pm A PackageManager from which the XML can be loaded; usually 
  205.      * the PackageManager from which you originally retrieved this item. 
  206.      * @param name Name of the meta-date you would like to load. 
  207.      *  
  208.      * @return Returns an XmlPullParser you can use to parse the XML file 
  209.      * assigned as the given meta-data.  If the meta-data name is not defined 
  210.      * or the XML resource could not be found, null is returned. 
  211.      */  
  212.     public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {  
  213.         if (metaData != null) {  
  214.             int resid = metaData.getInt(name);  
  215.             if (resid != 0) {  
  216.                 return pm.getXml(packageName, resid, getApplicationInfo());  
  217.             }  
  218.         }  
  219.         return null;  
  220.     }  
  221.   
  222.     protected void dumpFront(Printer pw, String prefix) {  
  223.         if (name != null) {  
  224.             pw.println(prefix + "name=" + name);  
  225.         }  
  226.         pw.println(prefix + "packageName=" + packageName);  
  227.         if (labelRes != 0 || nonLocalizedLabel != null || icon != 0) {  
  228.             pw.println(prefix + "labelRes=0x" + Integer.toHexString(labelRes)  
  229.                     + " nonLocalizedLabel=" + nonLocalizedLabel  
  230.                     + " icon=0x" + Integer.toHexString(icon));  
  231.         }  
  232.     }  
  233.       
  234.     protected void dumpBack(Printer pw, String prefix) {  
  235.         // no back here  
  236.     }  
  237.       
  238.     public void writeToParcel(Parcel dest, int parcelableFlags) {  
  239.         dest.writeString(name);  
  240.         dest.writeString(packageName);  
  241.         dest.writeInt(labelRes);  
  242.         TextUtils.writeToParcel(nonLocalizedLabel, dest, parcelableFlags);  
  243.         dest.writeInt(icon);  
  244.         dest.writeInt(logo);  
  245.         dest.writeBundle(metaData);  
  246.     }  
  247.       
  248.     protected PackageItemInfo(Parcel source) {  
  249.         name = source.readString();  
  250.         packageName = source.readString();  
  251.         labelRes = source.readInt();  
  252.         nonLocalizedLabel  
  253.                 = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);  
  254.         icon = source.readInt();  
  255.         logo = source.readInt();  
  256.         metaData = source.readBundle();  
  257.     }  
  258.   
  259.     /** 
  260.      * Get the ApplicationInfo for the application to which this item belongs, 
  261.      * if available, otherwise returns null. 
  262.      *  
  263.      * @return Returns the ApplicationInfo of this item, or null if not known. 
  264.      *  
  265.      * @hide 
  266.      */  
  267.     protected ApplicationInfo getApplicationInfo() {  
  268.         return null;  
  269.     }  
  270.   
  271.     public static class DisplayNameComparator  
  272.             implements Comparator<PackageItemInfo> {  
  273.         public DisplayNameComparator(PackageManager pm) {  
  274.             mPM = pm;  
  275.         }  
  276.   
  277.         public final int compare(PackageItemInfo aa, PackageItemInfo ab) {  
  278.             CharSequence  sa = aa.loadLabel(mPM);  
  279.             if (sa == null) sa = aa.name;  
  280.             CharSequence  sb = ab.loadLabel(mPM);  
  281.             if (sb == null) sb = ab.name;  
  282.             return sCollator.compare(sa.toString(), sb.toString());  
  283.         }  
  284.   
  285.         private final Collator   sCollator = Collator.getInstance();  
  286.         private PackageManager   mPM;  
  287.     }  
  288. }  
示例:

AnroidManifest.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.getmanifestdemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="3.2.1.32768" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <meta-data  
  17.             android:name="channel"  
  18.             android:value="$(CHANNEL)" />  
  19.   
  20.         <activity  
  21.             android:name="com.getmanifestdemo.MainActivity"  
  22.             android:label="@string/app_name" >  
  23.             <intent-filter>  
  24.                 <action android:name="android.intent.action.MAIN" />  
  25.   
  26.                 <category android:name="android.intent.category.LAUNCHER" />  
  27.             </intent-filter>  
  28.         </activity>  
  29.     </application>  
  30.   
  31. </manifest>  

解析代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. PackageManager pkgmanager = mContext.getPackageManager();  
  2.         try {  
  3.             String packageName = mContext.getPackageName();  
  4.             PackageInfo packageInfo = pkgmanager.getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);  
  5.             String versionName = packageInfo.versionName;  
  6.             Log.v("YAN""versionName:" + versionName);  
  7.             ApplicationInfo application = pkgmanager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);  
  8.             Bundle metaData = application.metaData;  
  9.             String channel = metaData.getString("IFLYTEK_CHANNEL");  
  10.             Log.v("YAN""channel name:" + channel);  
  11.               
  12.         } catch (NameNotFoundException e) {  
  13.             Log.v("YAN""channel name exception:" + e.getMessage());  
  14.             e.printStackTrace();  
  15.         }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android WebView 中,AppCache(Application Cache)是一种用于离线应用的缓存机制,它允许网页应用在离线状态下加载和使用资源。AppCache 使用一个描述文件(通常是 `manifest.appcache`)来指定要缓存的文件列表。 当 WebView 加载包含 AppCache 的网页时,它会解析 manifest.appcache 文件,并按照其中指定的文件列表下载和缓存相关资源。manifest.appcache 文件是一个简单的文本文件,其内容由以下几个部分组成: 1. CACHE MANIFEST:指示这是一个 AppCache 文件。 2. CACHE:列出需要缓存的文件列表。每行表示一个文件的路径,可以是相对路径或绝对路径。这些文件将被下载并存储在本地缓存中。 3. NETWORK:列出不会被缓存的文件列表。这些文件将始终从网络加载。 4. FALLBACK:指定离线状态下的备用资源。当某个文件无法从缓存中加载时,会尝试从 FALLBACK 中指定的资源进行替代。 示例 manifest.appcache 文件内容如下: ``` CACHE MANIFEST # Version: 1.0.0 CACHE: index.html styles.css script.js NETWORK: api.example.com FALLBACK: offline.html /offline.html ``` 上述示例中,index.html、styles.css 和 script.js 会被缓存,api.example.com 不会被缓存(每次都从网络加载),而 offline.html 将作为离线状态下的替代资源。 需要注意的是,AppCache 在 Android 4.4(KitKat)之后被废弃,推荐使用 Service Worker 和其他离线技术来替代。因此,在开发新的应用时,建议考虑使用更现代的离线方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值