主要Launcher這個類,一些可以意會的代碼:
一、有關過濾注冊了 <category Android:name="android.intent.category.LAUNCHER" />的應用
- Intent intent = new Intent(Intent.ACTION_MAIN, null);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
-
- PackageManager packageManager = getPackageManager();
- final List<ResolveInfo> apps = packageManager.queryIntentActivities(
- intent, 0);
- final int count = apps.size();
上面重點是應用注冊了“intent.addCategory(Intent.CATEGORY_LAUNCHER);”
即一般應用在AndroidManifest.xml中的應用入口Acitivity中會這樣寫到如下:
- <activity android:name=".USBActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
重點 <category android:name="android.intent.category.LAUNCHER" />而上面的
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
代碼就是過濾哪些應用注冊了android.intent.category.LAUNCHER
二、獲取app的一些相關信息
ResolveInfo這個類我們需要做一個了解
- List<Intent> intentToLaunchList = packageManager
- .getLaunchIntentListForPackage(packageName);
- if (intentToLaunchList != null) {
- for (int i = 0; i < intentToLaunchList.size(); i++) {
- Intent intentToLaunch = intentToLaunchList.get(i);
- ComponentName component = intentToLaunch.getComponent();
- PackageManager packageManager = context.getPackageManager();
- ActivityInfo activityInfo = null;
- try {
- activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
- } catch (NameNotFoundException e) {
- if (LauncherSettings.LOG_ON) Log.e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e);
- }
- 補充一個public class ApplicationInfo extends PackageItemInfo implements Parcelable
- public class ActivityInfo extends ComponentInfo
- implements Parcelable
-
- public class ComponentInfo extends PackageItemInfo
- public class PackageItemInfo 類中有這麼一個方法
-
- /**
- * Load an XML resource attached to the meta-data of this item. This will
- * retrieved the name meta-data entry, and if defined call back on the
- * given PackageManager to load its XML file from the application.
- *
- * @param pm A PackageManager from which the XML can be loaded; usually
- * the PackageManager from which you originally retrieved this item.
- * @param name Name of the meta-date you would like to load.
- *
- * @return Returns an XmlPullParser you can use to parse the XML file
- * assigned as the given meta-data. If the meta-data name is not defined
- * or the XML resource could not be found, null is returned.
- */
- public XmlResourceParser loadXmlMetaData(PackageManager pm, String name) {
- if (metaData != null) {
- int resid = metaData.getInt(name);
- if (resid != 0) {
- return pm.getXml(packageName, resid, null);
- }
- }
- return null;
- }
- public abstract XmlResourceParser getXml (String packageName, int resid, ApplicationInfo appInfo)
-
- Since: API Level 1
- Retrieve an XML file from a package. This is a low-level API used to retrieve XML meta data.
- Parameters
-
- packageName The name of the package that this xml is coming from. Can not be null.
- resid The resource identifier of the desired xml. Can not be 0.
- 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.
- Returns
-
- Returns an XmlPullParser allowing you to parse out the XML data. Returns null if the xml resource could not be found for any reason.
下面實例
- ActivityInfo ai = getPackageManager().getActivityInfo(
- getComponentName(), PackageManager.GET_META_DATA);
- parser = ai.loadXmlMetaData(getPackageManager(),
- ALIAS_META_DATA);
- public final String ALIAS_META_DATA = "android.app.alias";
- <application android:hasCode="false">
- <activity android:name="android.app.AliasActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <meta-data android:name="android.app.alias" android:resource="@xml/alias" />
- </activity>