【Android源碼-PMS】(二)ComponentInfo類

注:轉載請註明來自Nemo, http://blog.csdn.net/nemo__


一、包名
      android.content.pm.ComponentInfo
     父類:android.content.pm.PackageItemInfo
     子類(均在android.content.pm包中):ActivityInfoServiceInfo, ProviderInfo.


二、概述        
/**
 * Base class containing information common to all application components
 * ({@link ActivityInfo}, {@link ServiceInfo}).  This class is not intended
 * to be used by itself; it is simply here to share common definitions
 * between all application components.  As such, it does not itself
 * implement Parcelable, but does provide convenience methods to assist
 * in the implementation of Parcelable in subclasses.
 */
       ComponentInfo, 代表一個應用內組件(如ActivityInfo, ServiceInfo, ProviderInfo)通用信息的基類。一般不會直接使用該類,它設計是爲了不同應用的組件共享統一的定義。它沒有實現接口Parcelable, 但它提供了傳Parcel型的構造函數,以writeToParcel()方法給它的子類來實現ComponentInfo部這部分的成員的Parcel化。

       
三、主要成員  
public ApplicationInfo applicationInfo;
組件所在的application/package信息, <application>標籤得到

public String processName;
組件所運行的進程名,string型從"android:process"屬性得到,如不設置則爲applicationInfo.processName.

public int descriptionRes;
組件的描述,string型的資源id,從"android:description"屬性得到,如不設置則爲0.

public boolean enabled = true;
表明當前組件能否被實例化,boolean型,從"android:enabled"屬性得到。如果它所在的ApplicationInfo中enabled爲false, 則這處的設置無效。

public boolean exported = false;
表明當前組件能否被其它Application的組件啓動,boolean型,從"android:exported"屬性得到。
       如果當前組件沒有一個<intent-filter>則它默認爲false(沒有任何<intent-filter>表明要用組件的準確名稱來啓動), exported=false表明當前組件只能被當前應用內組件啓動,或有相同UID的應用。
       當然也可以用permission來限制外部應用對該組件的訪問。如果該組件有"android:permission"屬性,則訪問者必須有聲明該權限。當該組件無permission屬性而<application>標籤有聲明時,則訪問者必須有<application>標籤明的permission.


四、主要方法  
1. 構造函數
public ComponentInfo()
public ComponentInfo(ComponentInfo orig)
protected ComponentInfo(Parcel source)

2. public CharSequence loadLabel(PackageManager pm)
返回該組件的標籤,CharSequence類型,優先級依次:nonLocalizedLabel, labelRes, applicationInfo.nonLocalizedLabelapplicationInfo.labelRes

3. public boolean isEnabled()
當且僅當enabled和applicationInfo.enabled同時爲true, 返回true.

4. public final int getIconResource()
public final int getLogoResource()
public final int getBannerResource()
返回該組件的icon, logo, banner, int型的資源id, 如果某項爲0, 則返回applicationInfo對應的資源id.

5. public Drawable loadDefaultIcon(PackageManager pm)
protected Drawable loadDefaultBanner(PackageManager pm)
protected Drawable loadDefaultLogo(PackageManager pm)
返回該組件的默認icon, logo, banner, int型的資源id, 分別返回的是applicationInfo的loadIcon(), loadBanner(), loadLogo().

6. protected ApplicationInfo getApplicationInfo()
返回該組件的applicationInfo.

7. public void writeToParcel(Parcel dest, int parcelableFlags)
先調用父類PackageItemInfo的writeToParcel()方法,再完成自己的5個成員的Parcel化。


五、調用情況
1. PackageParser.Component
PackageParser中的組件基類Component,它的一個構造函數會傳入ComponentInfo的子類, 內部對它的成員進行賦值。PackageParser有六個組件繼承自Component基類。

2. ResolveInfo
ResolveInfo更像一個工廠類,它內部有成員ActivityInfo activityInfo, ServiceInfo serviceInfo, ProviderInfo providerInfo; 而這三個類均繼承自ComponentInfo,ResolveInfo也是通過ComponentInfo型引用來動態操作這三個組件信息類。

        ComponentInfo也沒有直接使用的情況,它更多是作爲一個基類動態地被賦予它三個子類的實例,來達到多態的效果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章