歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Launcher 源碼有關加載應用xml等資源文件研究

Launcher 源碼有關加載應用xml等資源文件研究

日期:2017/3/1 10:46:08   编辑:Linux編程

主要Launcher這個類,一些可以意會的代碼:

一、有關過濾注冊了 <category Android:name="android.intent.category.LAUNCHER" />的應用

  1. Intent intent = new Intent(Intent.ACTION_MAIN, null);
  2. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  3. PackageManager packageManager = getPackageManager();
  4. final List<ResolveInfo> apps = packageManager.queryIntentActivities(
  5. intent, 0);
  6. 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. public class ComponentInfo extends PackageItemInfo

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


  1. public abstract XmlResourceParser getXml (String packageName, int resid, ApplicationInfo appInfo)
  2. Since: API Level 1
  3. Retrieve an XML file from a package. This is a low-level API used to retrieve XML meta data.
  4. Parameters
  5. packageName The name of the package that this xml is coming from. Can not be null.
  6. resid The resource identifier of the desired xml. Can not be 0.
  7. 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.
  8. Returns
  9. 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>
Copyright © Linux教程網 All Rights Reserved