加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Launcher 源码有关加载应用xml等资源文件分析

发布时间:2021-11-24 18:28:36 所属栏目:教程 来源:互联网
导读:主要Launcher这个类,一些可以意会的代码: 一、有关过滤注册了 category Android:name=android.intent.category.LAUNCHER /的应用 Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); PackageManager pa
主要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>  
 

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读