Launcher 源码有关加载应用xml等资源文件研究


主要Launcher这个类,一些可以意会的代码:

一、有关过滤注册了  <category Android:name="android.intent.category.LAUNCHER" />的应用

  1. Intent intent = new Intent(Intent.ACTION_MAIN, null);  
  2.            intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  3.   
  4.            PackageManager packageManager = getPackageManager();  
  5.            final List<ResolveInfo> apps = packageManager.queryIntentActivities(  
  6.                    intent, 0);  
  7.            final int count = apps.size();  

上面重点是应用注册了“intent.addCategory(Intent.CATEGORY_LAUNCHER);”

即一般应用在AndroidManifest.xml中的应用入口Acitivity中会这样写到如下:

  1. <activity android:name=".USBActivity"  
  2.           android:label="@string/app_name">  
  3.     <intent-filter>  
  4.         <action android:name="android.intent.action.MAIN" />  
  5.         <category android:name="android.intent.category.LAUNCHER" />  
  6.     </intent-filter>  
  7. </activity>  
重点  <category android:name="android.intent.category.LAUNCHER" />而上面的
  1. intent.addCategory(Intent.CATEGORY_LAUNCHER);  
代码就是过滤哪些应用注册了android.intent.category.LAUNCHER

二、获取app的一些相关信息

ResolveInfo这个类我们需要做一个了解

  1. List<Intent> intentToLaunchList = packageManager  
  2.                   .getLaunchIntentListForPackage(packageName);  
  3.           if (intentToLaunchList != null) {  
  4.               for (int i = 0; i < intentToLaunchList.size(); i++) {  
  5.                   Intent intentToLaunch = intentToLaunchList.get(i);  

 
  1. ComponentName component = intentToLaunch.getComponent();  
  2.        PackageManager packageManager = context.getPackageManager();  
  3.        ActivityInfo activityInfo = null;  
  4.        try {  
  5.            activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);  
  6.        } catch (NameNotFoundException e) {  
  7.            if (LauncherSettings.LOG_ON) Log.e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);  
  8.        }  


 
  1. 补充一个public class ApplicationInfo extends PackageItemInfo implements Parcelable   

 
  1. public class ActivityInfo extends ComponentInfo  
  2.         implements Parcelable   
  3.   
  4. public class ComponentInfo extends PackageItemInfo   

 
  1. public class PackageItemInfo 类中有这么一个方法  
  2.   
  3. /** 
  4.    * Load an XML resource attached to the meta-data of this item.  This will 
  5.    * retrieved the name meta-data entry, and if defined call back on the 
  6.    * given PackageManager to load its XML file from the application. 
  7.    *  
  8.    * @param pm A PackageManager from which the XML can be loaded; usually 
  9.    * the PackageManager from which you originally retrieved this item. 
  10.    * @param name Name of the meta-date you would like to load. 
  11.    *  
  12.    * @return Returns an XmlPullParser you can use to parse the XML file 
  13.    * assigned as the given meta-data.  If the meta-data name is not defined 
  14.    * or the XML resource could not be found, null is returned. 
  15.    */  
  16.   public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {  
  17.       if (metaData != null) {  
  18.           int resid = metaData.getInt(name);  
  19.           if (resid != 0) {  
  20.               return pm.getXml(packageName, resid, null);  
  21.           }  
  22.       }  
  23.       return null;  
  24.   }  


 
  1. public abstract XmlResourceParser getXml (String packageName, int resid, ApplicationInfo appInfo)  
  2.   
  3. Since: API Level 1  
  4. Retrieve an XML file from a package. This is a low-level API used to retrieve XML meta data.  
  5. Parameters  
  6.   
  7. packageName The name of the package that this xml is coming from. Can not be null.  
  8. resid   The resource identifier of the desired xml. Can not be 0.  
  9. appInfo Overall information about packageName. This may be null, in which case the application information will be retrieved for you if needed; if you already have this information around, it can be much more efficient to supply it here.  
  10. Returns  
  11.   
  12. Returns an XmlPullParser allowing you to parse out the XML data. Returns null if the xml resource could not be found for any reason.  

下面实例

  1. ActivityInfo ai = getPackageManager().getActivityInfo(  
  2.                     getComponentName(), PackageManager.GET_META_DATA);  
  3.             parser = ai.loadXmlMetaData(getPackageManager(),  
  4.                     ALIAS_META_DATA);  
 
  1. public final String ALIAS_META_DATA = "android.app.alias";  
 
  1. <application android:hasCode="false">  
  2.     <activity android:name="android.app.AliasActivity" android:label="@string/app_name">  
  3.         <intent-filter>  
  4.             <action android:name="android.intent.action.MAIN" />  
  5.             <category android:name="android.intent.category.LAUNCHER" />  
  6.         </intent-filter>  
  7.         <meta-data android:name="android.app.alias" android:resource="@xml/alias" />  
  8.     </activity>  
  • 1
  • 2
  • 下一页

相关内容