主要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 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; }
下面实例
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>
三、ResolveInfo与activityInfo关系
public class ResolveInfo implements Parcelable { /** * The activity that corresponds to this resolution match, if this * resolution is for an activity. One and only one of this and * serviceInfo must be non-null. */ public ActivityInfo activityInfo;从上面流程可以清晰看出是怎么读取应用的资源的。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
补充:
public static ComponentName getHomeComponent(PackageManager PM) { Intent home_intent = new Intent("android.intent.action.MAIN"); home_intent.addCategory("android.intent.category.HOME"); home_intent.addCategory("android.intent.category.DEFAULT"); ComponentName cn = home_intent.resolveActivity(PM); if (cn == null) Log.v(TAG, "[Default] package:null"); else Log.v(TAG, "[Default] package:" + cn.getPackageName() + " class:" + cn.getClassName()); return cn; }
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); int listSize = list.size(); for (int i = 0; i < listSize; i++) { ResolveInfo resolveInfo = list.get(i); if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { Drawable icon = null; String title = null; String summary = null; // Get the activity's meta-data try { Resources res = pm .getResourcesForApplication(resolveInfo.activityInfo.packageName); Bundle metaData = resolveInfo.activityInfo.metaData; if (res != null && metaData != null) { icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); } } catch (NameNotFoundException e) { // Ignore } catch (NotFoundException e) { // Ignore } // Set the preference title to the activity's label if no // meta-data is found if (TextUtils.isEmpty(title)) { title = resolveInfo.loadLabel(pm).toString(); }
PackageManager pm = context.getPackageManager(); List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); int listSize = list.size(); for (int i = 0; i < listSize; i++) { ResolveInfo resolveInfo = list.get(i); if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { // Replace the intent with this specific activity preference.setIntent(new Intent().setClassName( resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name)); if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { // Set the preference title to the activity's label preference.setTitle(resolveInfo.loadLabel(pm)); }
// Resources res = packageManager // .getResourcesForApplication(resolveInfo.activityInfo.packageName); // xmlResourceParser = res.getXml(R.xml.voice_recognition); // ComponentName component = intent.getComponent(); // activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
android 特有的XmlResourceParser解析xml文件方式
http://www.ibm.com/developerworks/cn/xml/x-androidxml/
http://www.iteye.com/wiki/blog/1284853
http://blog.csdn.net/kmyhy/article/details/6439794
http://kahweh.blog.sohu.com/147050083.html
http://scrobin.blog.163.com/blog/static/1787064332011547239247/