APK安裝流程系列文章整體內(nèi)容如下:
- APK安裝流程詳解0——前言
- APK安裝流程詳解1——有關(guān)"安裝ing"的實(shí)體類概述
- APK安裝流程詳解2——PackageManager簡介
- APK安裝流程詳解3——PackageManager與PackageManagerService
- APK安裝流程詳解4——安裝中關(guān)于so庫的那些事
- APK安裝流程詳解5——PackageInstallerService和Installer
- APK安裝流程詳解6——PackageManagerService啟動前奏
- APK安裝流程詳解7——PackageManagerService的啟動流程(上)
- APK安裝流程詳解8——PackageManagerService的啟動流程(下)
- APK安裝流程詳解9——PackageParser解析APK(上)
- APK安裝流程詳解10——PackageParser解析APK(下)
- APK安裝流程詳解11——普通應(yīng)用安裝簡介
- APK安裝流程詳解12——PackageManagerService中的新安裝流程上(拷貝)
- APK安裝流程詳解13——PackageManagerService中的新安裝流程下(裝載)
- APK安裝流程詳解14——PMS中的新安裝流程上(拷貝)補(bǔ)充
- APK安裝流程詳解15——PMS中的新安裝流程下(裝載)補(bǔ)充
- APK安裝流程詳解16——Android包管理總結(jié)
本片文章主要內(nèi)容如下:
- 1、PackageManager介紹
- 2、PackageManager類概述
- 3、PackageManager與APK安裝
- 4、PackageManager的功能
- 5、PackageManager常用方法
- 6、PackageManager中關(guān)于"安裝"的幾個(gè)方法
俗話說的好,得中原者,得天下,那么想要了解Android的安裝了流程就不得不提及一個(gè)重要的類"PackageManager"我們就先來了解這兩個(gè)類
一、PackageManager介紹
Android系統(tǒng)為我們提供了很多服務(wù)的管理類,比如ActivityManager、PowrManager,那么和安裝APK有關(guān)就是PackageManager了,它負(fù)責(zé)管理應(yīng)用程序包,通過它就可以獲取應(yīng)用程序信息。
二、PackageManager類概述
這個(gè)類已經(jīng)5000多行,我們就不詳細(xì)介紹了,我們來看下這個(gè)類
/**
* Class for retrieving various kinds of information related to the application
* packages that are currently installed on the device.
*
* You can find this class through {@link Context#getPackageManager}.
*/
public abstract class PackageManager {
...
}
通過上面代碼我們知道這個(gè)類是抽象類,那我們來看下注釋
PackageManager這個(gè)類是檢測當(dāng)前已經(jīng)安裝在當(dāng)前設(shè)備上的應(yīng)用程序包的信息。你可以調(diào)用Context類的getPackageManager()方法來獲取PackageManager方法。
三、PackageManager與APK安裝
PackageManager是一個(gè)實(shí)際上管理應(yīng)用程序安裝、卸載和升級的API。當(dāng)我們安裝APK文件時(shí),PackageManager會解析APK包文件和顯示確認(rèn)信息。當(dāng)我們點(diǎn)擊OK按鈕后,PackageManager會調(diào)用一個(gè)叫"InstallPackage"的方法,這個(gè)方法有4個(gè)參數(shù),也就是uri、installFlags、observer、installPackagename。PackageManager會啟動一個(gè)叫"package"的servcie服務(wù),現(xiàn)在所有模糊的東西會發(fā)生在這個(gè)service中。
四、PackageManager的功能
- 1、安裝、卸載應(yīng)用
- 2、查詢permission相關(guān)信息
- 3、查詢Application相關(guān)信息(application、activity、receiver、service、provider及相應(yīng)屬性等)
- 4、查詢已安裝應(yīng)用
- 5、增加、刪除permission
- 6、清除用戶數(shù)據(jù)、緩存、代碼等
五、PackageManager常用方法
1、public abstract PackageInfo getPackageInfo(String packageName, int flags)方法:
通過包名獲取該包名對應(yīng)的應(yīng)用程序的PackageInfo對象
代碼在PackageManager.java2031行
/**
* Retrieve overall information about an application package that is
* installed on the system.
* <p>
* Throws {@link NameNotFoundException} if a package with the given name can
* not be found on the system.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of the
* desired package.
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES}, {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS}, {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS}, {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS}, {@link #GET_SERVICES},
* {@link #GET_SIGNATURES}, {@link #GET_UNINSTALLED_PACKAGES} to
* modify the data returned.
* @return Returns a PackageInfo object containing information about the
* package. If flag GET_UNINSTALLED_PACKAGES is set and if the
* package is not found in the list of installed applications, the
* package information is retrieved from the list of uninstalled
* applications (which includes installed applications as well as
* applications with data directory i.e. applications which had been
* deleted with {@code DONT_DELETE_DATA} flag set).
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
* @see #GET_UNINSTALLED_PACKAGES
*/
public abstract PackageInfo getPackageInfo(String packageName, int flags)
throws NameNotFoundException;
先翻譯一下注釋:
檢索出有關(guān)系統(tǒng)上安裝應(yīng)用程序包的總體信息
關(guān)于PackageInfo這各類請參考APK安裝流程詳解1——有關(guān)"安裝ing"的實(shí)體類概述
。它表示檢索有關(guān)系統(tǒng)上安裝的應(yīng)用程序包的總體信息。
2、public abstract String[] currentToCanonicalPackageNames(String[] names)方法:
代碼在PackageManager.java2041行
/**
* Map from the current package names in use on the device to whatever
* the current canonical name of that package is.
* @param names Array of current names to be mapped.
* @return Returns an array of the same size as the original, containing
* the canonical name for each package.
*/
public abstract String[] currentToCanonicalPackageNames(String[] names);
簡單翻譯注釋如下:
從設(shè)備上使用當(dāng)前包名映射到該軟件包名的當(dāng)前規(guī)范名稱。
- 入?yún)arams names 表示要映射的當(dāng)前名稱的數(shù)組
- 出參return 表示與原始數(shù)組大小相同的數(shù)組,其中包含每個(gè)包的規(guī)范名稱
如果修改包名會用到,沒有修改過包名一般不會用到
3、public abstract String[] canonicalToCurrentPackageNames(String[] names)方法:
主要是相對于上面的方法
代碼在PackageManager.java2049行
/**
* Map from a packages canonical name to the current name in use on the device.
* @param names Array of new names to be mapped.
* @return Returns an array of the same size as the original, containing
* the current name for each package.
*/
public abstract String[] canonicalToCurrentPackageNames(String[] names);
簡單翻譯注釋如下:
將軟件包規(guī)范名稱映射到設(shè)備上正在使用的當(dāng)前名稱。
- 入?yún)arams names 表示要映射的新名稱數(shù)組
- 出參return 表示返回與原始數(shù)組大小相同的數(shù)組,其中包含每個(gè)包的當(dāng)前名稱。
其中canonicalToCurrentPackageNames()和currentToCanonicalPackageNames()方法是相反的兩個(gè)方法
4、public abstract Intent getLaunchIntentForPackage(String packageName)方法:
獲取一個(gè)應(yīng)用程序的Lauch的Intent
代碼在PackageManager.java 2066行
/**
* Returns a "good" intent to launch a front-door activity in a package.
* This is used, for example, to implement an "open" button when browsing
* through packages. The current implementation looks first for a main
* activity in the category {@link Intent#CATEGORY_INFO}, and next for a
* main activity in the category {@link Intent#CATEGORY_LAUNCHER}. Returns
* <code>null</code> if neither are found.
*
* @param packageName The name of the package to inspect.
*
* @return A fully-qualified {@link Intent} that can be used to launch the
* main activity in the package. Returns <code>null</code> if the package
* does not contain such an activity, or if <em>packageName</em> is not
* recognized.
*/
public abstract Intent getLaunchIntentForPackage(String packageName);
返回一個(gè)"包"中的"入口"Activity的Intent,例如,這是類似于在瀏覽包的"打開"按鈕。這個(gè)當(dāng)前的安裝啟動第一步在category(CATEGORY_INFO)中尋找main Activity,然后在category(CATEGORY_LAUNCHER)尋找main Activity。如果找不到就返回null。
入?yún)⑹前?/p>
5、public abstract Intent getLeanbackLaunchIntentForPackage(String packageName)方法:
獲取一個(gè)TV應(yīng)用的Leanback的Intent
代碼在PackageManager.java2083行
/**
* Return a "good" intent to launch a front-door Leanback activity in a
* package, for use for example to implement an "open" button when browsing
* through packages. The current implementation will look for a main
* activity in the category {@link * return null if no main leanback activities are found.
* <p>
* Throws {@link NameNotFoundException} if a package with the given name
* cannot be found on the system.
*
* @param packageName The name of the package to inspect.
* @return Returns either a fully-qualified Intent that can be used to launch
* the main Leanback activity in the package, or null if the package
* does not contain such an activity.
*/
public abstract Intent getLeanbackLaunchIntentForPackage(String packageName);
Leanback activity一般在TV上使用的比較多,上面這個(gè)方法返回的Intent的一般在AndroidManifest如下:
<activity
android:name="com.example.android.TvActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Leanback">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
其實(shí)就是返回的是com.example.android.TvActivity的打開Intent
翻譯注釋如下:
返回一個(gè)"合適的"Intent,這個(gè)Intent是打開LeanbackActivity的入口Intent。例如,類似于在瀏覽包的"打開"按鈕。這個(gè)將找匹配CATEGORY_LEANBACK_LAUNCHER的Activity。如果沒有找到則返回null。
6、public abstract int[] getPackageGids(String packageName)方法:
獲取相應(yīng)包的Group ids
代碼在PackageManager.java2097行
/**
* Return an array of all of the POSIX secondary group IDs that have been
* assigned to the given package.
* <p>
* Note that the same package may have different GIDs under different
* {@link UserHandle} on the same device.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of the
* desired package.
* @return Returns an int array of the assigned GIDs, or null if there are
* none.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*/
public abstract int[] getPackageGids(String packageName)
throws NameNotFoundException;
翻譯注釋如下:
- 返回已分配給包的所有的POSIX輔助組ID的數(shù)組
- 請注意,相同的包可能會有不同的GID, 因?yàn)榭赡艽嬖谠谕粋€(gè)設(shè)備開啟了不同的"用戶模式“下
- 入?yún)arams packageName 是全包名
- 出參 表示 返回應(yīng)用程序?qū)?yīng)的GID的int 數(shù)組,如果沒有應(yīng)用程序,則返回null。
7、public abstract int[] getPackageUid(String packageName)方法:
獲取相應(yīng)包的UID
代碼在PackageManager.java2113行
/**
* Return the UID associated with the given package name.
* <p>
* Note that the same package will have different UIDs under different
* {@link UserHandle} on the same device.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of the
* desired package.
* @return Returns an integer UID who owns the given package name.
* @throws NameNotFoundException if a package with the given name can not be
* found on the system.
*/
public abstract int getPackageUid(String packageName, @PackageInfoFlags int flags)
throws NameNotFoundException;
翻譯注釋如下:
- 返回與給定包名的對應(yīng)的UID
- 請注意,相同的包可能會有不同的UID, 因?yàn)榭赡艽嬖谠谕粋€(gè)設(shè)備開啟了不同的"用戶模式“下
- 入?yún)arams packageName 是全包名
- 出參 表示 返回給定包名的int 型的UID
8、 public abstract PermissionInfo getPermissionInfo(String name, int flags)方法:
根據(jù)包名和指定的flags獲取指定的授權(quán)信息
代碼在PackageManager.java2130行
/**
* Retrieve all of the information we know about a particular permission.
*
* @param name The fully qualified name (i.e. com.google.permission.LOGIN)
* of the permission you are interested in.
* @param flags Additional option flags. Use {@link #GET_META_DATA} to
* retrieve any meta-data associated with the permission.
*
* @return Returns a {@link PermissionInfo} containing information about the
* permission.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* @see #GET_META_DATA
*/
public abstract PermissionInfo getPermissionInfo(String name, int flags)
throws NameNotFoundException;
翻譯注釋如下:
- 檢測出我們想要知道所有關(guān)于權(quán)限信息
- 入?yún)arams name 是權(quán)限的全名稱,比如:com.google.permission.LOGIN
- 入?yún)arams name 附加選項(xiàng)的標(biāo)志位,用來獲取檢索出與權(quán)限相關(guān)聯(lián)的元數(shù)據(jù)(通過使用"GET_META_DATA")
- 出參 表示 返回權(quán)限信息的對象,里面包含我們關(guān)于權(quán)限信息的的所有信息。
10、 public abstract List<PermissionInfo> queryPermissionsByGroup(String group,int flags)方法:
獲取所有的PermissionInfo集合
代碼在PackageManager.java2148行
/**
* Query for all of the permissions associated with a particular group.
*
* @param group The fully qualified name (i.e. com.google.permission.LOGIN)
* of the permission group you are interested in. Use null to
* find all of the permissions not associated with a group.
* @param flags Additional option flags. Use {@link #GET_META_DATA} to
* retrieve any meta-data associated with the permissions.
*
* @return Returns a list of {@link PermissionInfo} containing information
* about all of the permissions in the given group.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* @see #GET_META_DATA
*/
public abstract List<PermissionInfo> queryPermissionsByGroup(String group,int flags) throws NameNotFoundException;
翻譯注釋如下:
- 查詢與特定組相關(guān)的所有權(quán)限
- 入?yún)arams group 需要查詢組的全名稱,例如:com.google.permission.LOGIN,如果使用NULL則可以查詢與組無關(guān)的所有權(quán)限
- 入?yún)arams name 附加選項(xiàng)的標(biāo)志位,用來獲取檢索出與權(quán)限相關(guān)聯(lián)的的元數(shù)據(jù)(通過使用"GET_META_DATA")
- 出參 表示 返回權(quán)限信息的對象的集合
10、 public abstract List<PermissionInfo> queryPermissionsByGroup(String group,int flags)方法:
根據(jù)指定Group明和獲取PermissionGroupInfo對象。
代碼在PackageManager.java2166行
/**
* Retrieve all of the information we know about a particular group of
* permissions.
*
* @param name The fully qualified name (i.e. com.google.permission_group.APPS)
* of the permission you are interested in.
* @param flags Additional option flags. Use {@link #GET_META_DATA} to
* retrieve any meta-data associated with the permission group.
*
* @return Returns a {@link PermissionGroupInfo} containing information
* about the permission.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* @see #GET_META_DATA
*/
public abstract PermissionGroupInfo getPermissionGroupInfo(String name,int flags) throws NameNotFoundException;
翻譯注釋如下:
- 檢索出我們知道的關(guān)于一組特殊權(quán)限的所有信息
- 入?yún)arams name 一組權(quán)限的全限定名稱,例如com.google.permission_group.APPS
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,用來獲取檢索出與權(quán)限相關(guān)聯(lián)的的元數(shù)據(jù)(通過使用"GET_META_DATA")
- 出參 表示 返回一個(gè)包含權(quán)限組信息的PermissionGroupInfo對象
11、public abstract List<PermissionGroupInfo> getAllPermissionGroups( int flags)方法:
獲取所有的PermissGroup集合
代碼在PackageManager.java2178行
/**
* Retrieve all of the known permission groups in the system.
*
* @param flags Additional option flags. Use {@link #GET_META_DATA} to
* retrieve any meta-data associated with the permission group.
*
* @return Returns a list of {@link PermissionGroupInfo} containing
* information about all of the known permission groups.
*
* @see #GET_META_DATA
*/
public abstract List<PermissionGroupInfo> getAllPermissionGroups(
@PermissionGroupInfoFlags int flags);
翻譯注釋如下:
- 檢索出系統(tǒng)中所有已知的權(quán)限
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,用來獲取檢索出與權(quán)限相關(guān)聯(lián)的的元數(shù)據(jù)(通過使用"GET_META_DATA")
- 出參 表示 返回有關(guān)所有權(quán)限的組的信息
12、public abstract ApplicationInfo getApplicationInfo(String packageName,int flags) throws NameNotFoundException;方法:
根據(jù)包名返回其對應(yīng)的ApplicationInfo信息
代碼在PackageManager.java2207行
/**
* Retrieve all of the information we know about a particular
* package/application.
*
* @param packageName The full name (i.e. com.google.apps.contacts) of an
* application.
* @param flags Additional option flags. Use any combination of
* {@link #GET_META_DATA}, {@link #GET_SHARED_LIBRARY_FILES},
* {@link #MATCH_SYSTEM_ONLY}, {@link #MATCH_UNINSTALLED_PACKAGES}
* to modify the data returned.
*
* @return An {@link ApplicationInfo} containing information about the
* package. If flag {@code MATCH_UNINSTALLED_PACKAGES} is set and if the
* package is not found in the list of installed applications, the
* application information is retrieved from the list of uninstalled
* applications (which includes installed applications as well as
* applications with data directory i.e. applications which had been
* deleted with {@code DONT_DELETE_DATA} flag set).
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*
* @see #GET_META_DATA
* @see #GET_SHARED_LIBRARY_FILES
* @see #MATCH_DISABLED_UNTIL_USED_COMPONENTS
* @see #MATCH_SYSTEM_ONLY
* @see #MATCH_UNINSTALLED_PACKAGES
*/
public abstract ApplicationInfo getApplicationInfo(String packageName,
int flags) throws NameNotFoundException;
翻譯注釋如下:
- 檢索出一個(gè)應(yīng)用程序的所有信息(ApplicationInfo)
- 入?yún)arams packageName 包全名例如com.google.apps.contacts
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,可以使用下面這四個(gè)的任何組合過濾返回值
- GET_META_DATA :ComponentInfo的標(biāo)志位,返回與該組件(ComponentInfo)相關(guān)聯(lián)的(metaData)數(shù)據(jù)(android.os.Bundle)。
- GET_SHARED_LIBRARY_FILES:ApplicationInfo的標(biāo)志位,返回與應(yīng)用程序關(guān)聯(lián)的共享庫(ApplicationInfo路徑)
- MATCH_DISABLED_UNTIL_USED_COMPONENTS:PackageInfo的標(biāo)志位,表示包含禁用的組件。如果已處于禁用狀態(tài)程序?qū)⒆兏鼮閱⒂谩?/li>
- MATCH_SYSTEM_ONLYL:查詢標(biāo)志,僅包含有系統(tǒng)的應(yīng)用程序組件
- MATCH_UNINSTALLED_PACKAGES:參數(shù)標(biāo)志位,表示檢索出所有有數(shù)據(jù)的目錄的應(yīng)用程序(主要是卸載的)的信息
- 出參 表示 返回一個(gè)ApplicationInfo,里面有關(guān)包的所有信息。
13、public abstract ApplicationInfo getApplicationInfo(String packageName,int flags) throws NameNotFoundException;方法:
根據(jù)組件和要求返回特定的ActivityInfo
代碼在PackageManager.java2230行
/**
* Retrieve all of the information we know about a particular activity
* class.
*
* @param component The full component name (i.e.
* com.google.apps.contacts/com.google.apps.contacts.
* ContactsList) of an Activity class.
* @param flags Additional option flags. Use any combination of
* {@link #GET_META_DATA}, {@link #GET_SHARED_LIBRARY_FILES},{@GET_INTENT_FILTERS}
*
* returned.
* @return An {@link ActivityInfo} containing information about the
* activity.
* @throws NameNotFoundException if a package with the given name cannot be
* found on the system.
*/
public abstract ActivityInfo getActivityInfo(ComponentName component,int flags) throws NameNotFoundException;
翻譯注釋如下:
- 檢索出一個(gè)特定的Activity類的所有信息
- 入?yún)arams component 組件的全名稱例如:com.google.apps.contacts/com.google.apps.contacts. ContactsList中的一個(gè)Activity類
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_META_DATA :ComponentInfo的標(biāo)志位,返回與該組件(ComponentInfo)相關(guān)聯(lián)的(metaData)數(shù)據(jù)(android.os.Bundle)。
- GET_SHARED_LIBRARY_FILES:ApplicationInfo的標(biāo)志位,返回與應(yīng)用程序關(guān)聯(lián)的共享庫(ApplicationInfo路徑)
- GET_INTENT_FILTERS:包的標(biāo)志位,返回支持IntentFilter的相關(guān)組件。
- 出參 表示 返回一個(gè)ActivityInfo,里面包含類的所有信息。
14、public abstract ActivityInfo getReceiverInfo(ComponentName component, int flags)方法:
根據(jù)組件和要求返回特定的ActivityInfo
代碼在PackageManager.java2253行
/**
* Retrieve all of the information we know about a particular receiver
* class.
*
* <p>Throws {@link NameNotFoundException} if a receiver with the given
* class name cannot be found on the system.
*
* @param component The full component name (i.e.
* com.google.apps.calendar/com.google.apps.calendar.CalendarAlarm) of a Receiver
* class.
* @param flags Additional option flags. Use any combination of
* {@link #GET_META_DATA}, {@link #GET_SHARED_LIBRARY_FILES},
* to modify the data returned.
*
* @return {@link ActivityInfo} containing information about the receiver.
*
* @see #GET_INTENT_FILTERS
* @see #GET_META_DATA
* @see #GET_SHARED_LIBRARY_FILES
*/
public abstract ActivityInfo getReceiverInfo(ComponentName component,
int flags) throws NameNotFoundException;
翻譯注釋如下:
- 檢索出一個(gè)特定的Receiver類的所有信息(這里主要指ActivityInfo)
- 入?yún)arams component 組件的全名稱例如:com.google.apps.calendar/com.google.apps.calendar.CalendarAlarm中的一個(gè)Receiver類
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_META_DATA :ComponentInfo的標(biāo)志位,返回與該組件(ComponentInfo)相關(guān)聯(lián)的(metaData)數(shù)據(jù)(android.os.Bundle)。
- GET_SHARED_LIBRARY_FILES:ApplicationInfo的標(biāo)志位,返回與應(yīng)用程序關(guān)聯(lián)的共享庫(ApplicationInfo路徑)
- 出參 表示 返回一個(gè)ActivityInfo,里面包含Receiver類的所有信息。
15、public abstract ServiceInfo getServiceInfo(ComponentName component, int flags))方法:
根據(jù)組件和要求返回特定的ServiceInfo
代碼在PackageManager.java2275行
/**
* Retrieve all of the information we know about a particular service
* class.
*
* <p>Throws {@link NameNotFoundException} if a service with the given
* class name cannot be found on the system.
*
* @param component The full component name (i.e.
* com.google.apps.media/com.google.apps.media.BackgroundPlayback) of a Service
* class.
* @param flags Additional option flags. Use any combination of
* {@link #GET_META_DATA}, {@link #GET_SHARED_LIBRARY_FILES},
* to modify the data returned.
*
* @return ServiceInfo containing information about the service.
*
* @see #GET_META_DATA
* @see #GET_SHARED_LIBRARY_FILES
*/
public abstract ServiceInfo getServiceInfo(ComponentName component,
int flags) throws NameNotFoundException;
翻譯注釋如下:
- 檢索出一個(gè)特定的Service類的所有信息(這里主要指ServiceInfo)
- 入?yún)arams component 組件的全名稱例如:com.google.apps.media/com.google.apps.media.BackgroundPlayback中的一個(gè)Service類
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_META_DATA :ComponentInfo的標(biāo)志位,返回與該組件(ComponentInfo)相關(guān)聯(lián)的(metaData)數(shù)據(jù)(android.os.Bundle)。
- GET_SHARED_LIBRARY_FILES:ApplicationInfo的標(biāo)志位,返回與應(yīng)用程序關(guān)聯(lián)的共享庫(ApplicationInfo路徑)
- 出參 表示 返回一個(gè)ServiceInfo,里面包含Service類的所有信息。
16、public abstract ProviderInfo getProviderInfo(ComponentName component, int flags) 方法:
根據(jù)組件和要求返回特定的ProviderInfo
代碼在PackageManager.java2297行
/**
* Retrieve all of the information we know about a particular content
* provider class.
*
* <p>Throws {@link NameNotFoundException} if a provider with the given
* class name cannot be found on the system.
*
* @param component The full component name (i.e.
* com.google.providers.media/com.google.providers.media.MediaProvider) of a
* ContentProvider class.
* @param flags Additional option flags. Use any combination of
* {@link #GET_META_DATA}, {@link #GET_SHARED_LIBRARY_FILES},
* to modify the data returned.
*
* @return ProviderInfo containing information about the service.
*
* @see #GET_META_DATA
* @see #GET_SHARED_LIBRARY_FILES
*/
public abstract ProviderInfo getProviderInfo(ComponentName component,
int flags) throws NameNotFoundException;
翻譯注釋如下:
- 檢索出一個(gè)特定的content provider類的所有信息(這里主要指ProviderInfo)
- 入?yún)arams component 組件的全名稱例如:com.google.providers.media/com.google.providers.media.MediaProvider中的一個(gè)ContentProvider類
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_META_DATA :ComponentInfo的標(biāo)志位,返回與該組件(ComponentInfo)相關(guān)聯(lián)的(metaData)數(shù)據(jù)(android.os.Bundle)。
- GET_SHARED_LIBRARY_FILES:ApplicationInfo的標(biāo)志位,返回與應(yīng)用程序關(guān)聯(lián)的共享庫(ApplicationInfo路徑)
- 出參 表示 返回一個(gè)ProviderInfo。
17、public abstract ProviderInfo getProviderInfo(ComponentName component, int flags) 方法:
獲取設(shè)備上安裝的所有軟件包
代碼在PackageManager.java2334行
/**
* Return a List of all packages that are installed
* on the device.
*
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES},
* {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS},
* {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS},
* {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS},
* {@link #GET_SERVICES},
* {@link #GET_SIGNATURES},
* {@link #GET_UNINSTALLED_PACKAGES} to modify the data returned.
*
* @return A List of PackageInfo objects, one for each package that is
* installed on the device. In the unlikely case of there being no
* installed packages, an empty list is returned.
* If flag GET_UNINSTALLED_PACKAGES is set, a list of all
* applications including those deleted with {@code DONT_DELETE_DATA}
* (partially installed apps with data directory) will be returned.
*
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
* @see #GET_UNINSTALLED_PACKAGES
*/
public abstract List<PackageInfo> getInstalledPackages(int flags);
翻譯注釋如下:
- 返回設(shè)備上所有已經(jīng)安裝的應(yīng)用程序集合
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_ACTIVITIES :(packageInfo的標(biāo)志)表示 返回包(packageInfo)中包含的所有Activity信息
- GET_GIDS :(packageInfo的標(biāo)志)表示 返回關(guān)聯(lián)的GID(groupId)
- GET_CONFIGURATIONS :(packageInfo的標(biāo)志)表示 配置選項(xiàng)信息
- GET_INSTRUMENTATION :(PackageInfo的標(biāo)志)表示 是否使用了instrumentation
- GET_PERMISSIONS :(PackageInfo的標(biāo)志)表示 是否使用了permissions
- GET_PROVIDERS :(PackageInfo的標(biāo)志)表示 是否使用了providers
- GET_RECEIVERS :(PackageInfo的標(biāo)志)表示 是否使用了recevier
- GET_SERVICES :(PackageInfo的標(biāo)志)表示 是否使用了service
- GET_SIGNATURES :(PackageInf的標(biāo)志) 表示是否使用包的簽名信息
- GET_UNINSTALLED_PACKAGES:參數(shù)標(biāo)志位,表示檢索出所有有數(shù)據(jù)的目錄的應(yīng)用程序(主要是卸載的)的信息
- 出參 表示 PackageInfo的List集合
18、public abstract List<PackageInfo> getPackagesHoldingPermissions(String[] permissions, int flags)方法:
獲取具有特定權(quán)限的PackageInfo
代碼在PackageManager.java2366行
/**
* Return a List of all installed packages that are currently
* holding any of the given permissions.
*
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES},
* {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS},
* {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS},
* {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS},
* {@link #GET_SERVICES},
* {@link #GET_SIGNATURES},
* {@link #GET_UNINSTALLED_PACKAGES} to modify the data returned.
*
* @return Returns a List of PackageInfo objects, one for each installed
* application that is holding any of the permissions that were provided.
*
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
* @see #GET_UNINSTALLED_PACKAGES
*/
public abstract List<PackageInfo> getPackagesHoldingPermissions(
String[] permissions, int flags);
翻譯注釋如下:
- 返回當(dāng)前設(shè)備上所有已安裝應(yīng)用程序中的具有一些特殊權(quán)限的安裝包集合
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_ACTIVITIES :(packageInfo的標(biāo)志)表示 返回包(packageInfo)中包含的所有Activity信息
- GET_GIDS :(packageInfo的標(biāo)志)表示 返回關(guān)聯(lián)的GID(groupId)
- GET_CONFIGURATIONS :(packageInfo的標(biāo)志)表示 配置選項(xiàng)信息
- GET_INSTRUMENTATION :(PackageInfo的標(biāo)志)表示 是否使用了instrumentation
- GET_PERMISSIONS :(PackageInfo的標(biāo)志)表示 是否使用了permissions
- GET_PROVIDERS :(PackageInfo的標(biāo)志)表示 是否使用了providers
- GET_RECEIVERS :(PackageInfo的標(biāo)志)表示 是否使用了recevier
- GET_SERVICES :(PackageInfo的標(biāo)志)表示 是否使用了service
- GET_SIGNATURES :(PackageInf的標(biāo)志) 表示是否使用包的簽名信息
- GET_UNINSTALLED_PACKAGES:參數(shù)標(biāo)志位,表示檢索出所有有數(shù)據(jù)的目錄的應(yīng)用程序(主要是卸載的)的信息
- 出參 表示 PackageInfo的List集合
19、public abstract List<PackageInfo> getInstalledPackages(int flags, int userId)方法:
獲取具有特定用戶的PackageInfo
代碼在PackageManager.java2406行
/**
* Return a List of all packages that are installed on the device, for a specific user.
* Requesting a list of installed packages for another user
* will require the permission INTERACT_ACROSS_USERS_FULL.
* @param flags Additional option flags. Use any combination of
* {@link #GET_ACTIVITIES},
* {@link #GET_GIDS},
* {@link #GET_CONFIGURATIONS},
* {@link #GET_INSTRUMENTATION},
* {@link #GET_PERMISSIONS},
* {@link #GET_PROVIDERS},
* {@link #GET_RECEIVERS},
* {@link #GET_SERVICES},
* {@link #GET_SIGNATURES},
* {@link #GET_UNINSTALLED_PACKAGES} to modify the data returned.
* @param userId The user for whom the installed packages are to be listed
*
* @return A List of PackageInfo objects, one for each package that is
* installed on the device. In the unlikely case of there being no
* installed packages, an empty list is returned.
* If flag GET_UNINSTALLED_PACKAGES is set, a list of all
* applications including those deleted with {@code DONT_DELETE_DATA}
* (partially installed apps with data directory) will be returned.
*
* @see #GET_ACTIVITIES
* @see #GET_GIDS
* @see #GET_CONFIGURATIONS
* @see #GET_INSTRUMENTATION
* @see #GET_PERMISSIONS
* @see #GET_PROVIDERS
* @see #GET_RECEIVERS
* @see #GET_SERVICES
* @see #GET_SIGNATURES
* @see #GET_UNINSTALLED_PACKAGES
*
* @hide
*/
public abstract List<PackageInfo> getInstalledPackages(int flags, int userId);
翻譯注釋如下:
- 返回當(dāng)前設(shè)備上某個(gè)用戶的所有安裝軟件的安裝包信息,這里要求一個(gè)INTERACT_ACROSS_USERS_FULL權(quán)限
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_ACTIVITIES :(packageInfo的標(biāo)志)表示 返回包(packageInfo)中包含的所有Activity信息
- GET_GIDS :(packageInfo的標(biāo)志)表示 返回關(guān)聯(lián)的GID(groupId)
- GET_CONFIGURATIONS :(packageInfo的標(biāo)志)表示 配置選項(xiàng)信息
- GET_INSTRUMENTATION :(PackageInfo的標(biāo)志)表示 是否使用了instrumentation
- GET_PERMISSIONS :(PackageInfo的標(biāo)志)表示 是否使用了permissions
- GET_PROVIDERS :(PackageInfo的標(biāo)志)表示 是否使用了providers
- GET_RECEIVERS :(PackageInfo的標(biāo)志)表示 是否使用了recevier
- GET_SERVICES :(PackageInfo的標(biāo)志)表示 是否使用了service
- GET_SIGNATURES :(PackageInf的標(biāo)志) 表示是否使用包的簽名信息
- GET_UNINSTALLED_PACKAGES:參數(shù)標(biāo)志位,表示檢索出所有有數(shù)據(jù)的目錄的應(yīng)用程序(主要是卸載的)的信息
- 入?yún)arams userId 用戶的id
- 出參 PackageInfo對象的List集合,返回的每一個(gè)包都是安裝在這個(gè)設(shè)備上。如果一個(gè)安裝包都沒有,則返回一個(gè)空的List,當(dāng)然這種情況不太可能發(fā)生。如果設(shè)置了GET_UNINSTALLED_PACKAGES標(biāo)志位,則List包含使用DONT_DELETE_DATA標(biāo)志的已經(jīng)刪除的應(yīng)用程序。
20、public abstract List<ApplicationInfo> getInstalledApplications(int flags)方法:
獲取所有已經(jīng)安裝的應(yīng)用程序集合
代碼在PackageManager.java2746行
/**
* Return a List of all application packages that are installed on the
* device. If flag GET_UNINSTALLED_PACKAGES has been set, a list of all
* applications including those deleted with {@code DONT_DELETE_DATA} (partially
* installed apps with data directory) will be returned.
*
* @param flags Additional option flags. Use any combination of
* {@link #GET_META_DATA}, {@link #GET_SHARED_LIBRARY_FILES},
* {@link #GET_UNINSTALLED_PACKAGES} to modify the data returned.
*
* @return Returns a List of ApplicationInfo objects, one for each application that
* is installed on the device. In the unlikely case of there being
* no installed applications, an empty list is returned.
* If flag GET_UNINSTALLED_PACKAGES is set, a list of all
* applications including those deleted with {@code DONT_DELETE_DATA}
* (partially installed apps with data directory) will be returned.
*
* @see #GET_META_DATA
* @see #GET_SHARED_LIBRARY_FILES
* @see #GET_UNINSTALLED_PACKAGES
*/
public abstract List<ApplicationInfo> getInstalledApplications(int flags);
翻譯注釋如下:
- 返回設(shè)備上已經(jīng)安裝的所有應(yīng)用程序的集合。如果設(shè)置了GET_UNINSTALLED_PACKAGES標(biāo)志位,則集合中包含已經(jīng)設(shè)置為DONT_DELETE_DATA的已經(jīng)卸載的應(yīng)用程序。
- 入?yún)arams flags 附加選項(xiàng)的標(biāo)志位,你可以理解為篩選條件,可以使用的標(biāo)志位為:
- GET_META_DATA :ComponentInfo的標(biāo)志位,返回與該組件(ComponentInfo)相關(guān)聯(lián)的(metaData)數(shù)據(jù)(android.os.Bundle)。
- GET_SHARED_LIBRARY_FILES:ApplicationInfo的標(biāo)志位,返回與應(yīng)用程序關(guān)聯(lián)的共享庫(ApplicationInfo路徑)
- GET_UNINSTALLED_PACKAGES:參數(shù)標(biāo)志位,表示檢索出所有有數(shù)據(jù)的目錄的應(yīng)用程序(主要是卸載的)的信息
- 出參 ApplicationInfo對象的List集合,返回的每一個(gè)ApplicationInfo都是安裝在這個(gè)設(shè)備上。如果一個(gè)安裝ApplicationInfo都沒有,則返回一個(gè)空的List,當(dāng)然這種情況不太可能發(fā)生。如果設(shè)置了GET_UNINSTALLED_PACKAGES標(biāo)志位,則List包含使用DONT_DELETE_DATA標(biāo)志的已經(jīng)刪除的應(yīng)用程序。
六、PackageManager中關(guān)于"安裝"的幾個(gè)方法
1、public abstract void installPackage(Uri, IPackageInstallObserver, int,String)方法:
代碼在PackageManager.java 3586行
/**
* @hide Install a package. Since this may take a little while, the result
* will be posted back to the given observer. An installation will
* fail if the calling context lacks the
* {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if
* the package named in the package file's manifest is already
* installed, or if there's no space available on the device.
* @param packageURI The location of the package file to install. This can
* be a 'file:' or a 'content:' URI.
* @param observer An observer callback to get notified when the package
* installation is complete.
* {@link IPackageInstallObserver#packageInstalled(String, int)}
* will be called when that happens. This parameter must not be
* null.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING},
* {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that
* is performing the installation. This identifies which market
* the package came from.
* @deprecated Use {@link #installPackage(Uri, PackageInstallObserver, int,
* String)} instead. This method will continue to be supported
* but the older observer interface will not get additional
* failure details.
*/
// @SystemApi
public abstract void installPackage(
Uri packageURI, IPackageInstallObserver observer, int flags,
String installerPackageName);
通過代碼我們發(fā)現(xiàn)它是一個(gè) 系統(tǒng)API(SystemApi)
翻譯注釋如下:
- 安裝一個(gè)安裝包的時(shí)候,需要經(jīng)過一定的時(shí)間之后才能把安裝的結(jié)果返回一個(gè)觀察者。如果在安裝并調(diào)用Context的時(shí)候 在android.Manifest.permission缺少INSTALL_PACKAGES權(quán)限將會導(dǎo)致安裝失敗。如果設(shè)備上已經(jīng)安裝了這個(gè)同一個(gè)包名的應(yīng)用程序或者在設(shè)備已經(jīng)沒有了合適的空間都會導(dǎo)致安裝失敗。
- 入?yún)?packageURI :表示安裝的路徑,可以是"file:"或者"content:"的URI
- 入?yún)?observer :一個(gè)回調(diào)的觀察者,有了這個(gè)觀察者,就可以在軟件包安裝完成后得到安裝結(jié)果的通知。如果安裝完成會調(diào)用這個(gè)觀察者IPackageInstallObserver的packageInstalled(String,int)方法。observer這個(gè)入?yún)⒉荒転榭铡?/li>
- 入?yún)?flags :標(biāo)志位參數(shù),可能是以下的幾個(gè)值
- INSTALL_FORWARD_LOCK:安裝時(shí)候的標(biāo)志位,表示應(yīng)用程序?yàn)橄蚯版i定,即僅應(yīng)用程序本身可以訪問其代碼和非資源的assets
- INSTALL_REPLACE_EXISTING:安裝時(shí)候的標(biāo)志位,表示如果在設(shè)備存在同一個(gè)包名的安裝包,則你要替換已安裝的軟件包。
- INSTALL_ALLOW_TEST:安裝時(shí)候的標(biāo)志位,表示是否允許安裝測試包(在AndroidManifest里面設(shè)置了android:testOnly)
- 入?yún)?installerPackageName :正在進(jìn)行安裝的安裝包包名
- 注意事項(xiàng):不推薦使用這個(gè)方法(@deprecated):
建議使用installPackage(Uri,PackageInstallObserver,int,String)這個(gè)方法,在后續(xù)版本將支持installPackage(Uri,PackageInstallObserver,int,String)這個(gè)方法,因?yàn)槔习姹镜膐bserver無法獲得額外的故障細(xì)節(jié)。
2、 public abstract void installPackageWithVerification(Uri,IPackageInstallObserver, int, String,Uri, ManifestDigest,ContainerEncryptionParams);方法:
代碼在PackageManager.java 3624行
/**
* Similar to
* {@link #installPackage(Uri, IPackageInstallObserver, int, String)} but
* with an extra verification file provided.
*
* @param packageURI The location of the package file to install. This can
* be a 'file:' or a 'content:' URI.
* @param observer An observer callback to get notified when the package
* installation is complete.
* {@link IPackageInstallObserver#packageInstalled(String, int)}
* will be called when that happens. This parameter must not be
* null.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING},
* {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that
* is performing the installation. This identifies which market
* the package came from.
* @param verificationURI The location of the supplementary verification
* file. This can be a 'file:' or a 'content:' URI. May be
* {@code null}.
* @param manifestDigest an object that holds the digest of the package
* which can be used to verify ownership. May be {@code null}.
* @param encryptionParams if the package to be installed is encrypted,
* these parameters describing the encryption and authentication
* used. May be {@code null}.
* @hide
* @deprecated Use {@link #installPackageWithVerification(Uri,
* PackageInstallObserver, int, String, Uri, ManifestDigest,
* ContainerEncryptionParams)} instead. This method will
* continue to be supported but the older observer interface
* will not get additional failure details.
*/
// @SystemApi
public abstract void installPackageWithVerification(Uri packageURI,
IPackageInstallObserver observer, int flags, String installerPackageName,
Uri verificationURI, ManifestDigest manifestDigest,
ContainerEncryptionParams encryptionParams);
通過代碼我們發(fā)現(xiàn)它是一個(gè) 系統(tǒng)API(SystemApi)
翻譯注釋如下:
- 和installPackage(Uri,IPackageInstallObserver,int,String)方法類似,就是比它多了一個(gè)額外的文件驗(yàn)證功能
- 入?yún)?packageURI :表示安裝的路徑,可以是"file:"或者"content:"的URI
- 入?yún)?observer :一個(gè)回調(diào)的觀察者,有了這個(gè)觀察者,就可以在軟件包安裝完成后得到安裝結(jié)果的通知。如果安裝完成會調(diào)用這個(gè)觀察者IPackageInstallObserver的packageInstalled(String,int)方法。observer這個(gè)入?yún)⒉荒転榭铡?/li>
- 入?yún)?flags :標(biāo)志位參數(shù),可能是以下的幾個(gè)值
- INSTALL_FORWARD_LOCK:安裝時(shí)候的標(biāo)志位,表示應(yīng)用程序?yàn)橄蚯版i定,即僅應(yīng)用程序本身可以訪問其代碼和非資源的assets
- INSTALL_REPLACE_EXISTING:安裝時(shí)候的標(biāo)志位,表示如果在設(shè)備存在同一個(gè)包名的安裝包,則你要替換已安裝的軟件包。
- INSTALL_ALLOW_TEST:安裝時(shí)候的標(biāo)志位,表示是否允許安裝測試包(在AndroidManifest里面設(shè)置了android:testOnly)
- 入?yún)?installerPackageName :正在進(jìn)行安裝的安裝包包名
- 入?yún)?verificationURI :驗(yàn)證文件的位置,可以是"file:"或者"content:"的URI,該入?yún)⒖赡転閚ull
- 入?yún)?manifestDigest :一個(gè)包含可用于驗(yàn)證所有權(quán)的包的摘要的對象,該入?yún)⒖赡転閚ull
- 入?yún)?encryptionParams :一個(gè)描述加密和認(rèn)證狀態(tài)的對象,這個(gè)入?yún)⒛転閚ull。
- 注意事項(xiàng):不推薦使用這個(gè)方法(@deprecated):
建議使用installPackageWithVerification(Uri,PackageInstallObserver, int, String, Uri, ManifestDigest,ContainerEncryptionParams)這個(gè)方法,在后續(xù)版本將支持installPackageWithVerification(Uri,PackageInstallObserver, int, String, Uri, ManifestDigest,ContainerEncryptionParams)這個(gè)方法,因?yàn)槔习姹镜膐bserver無法獲得額外的故障細(xì)節(jié)。
3、public abstract void installPackageWithVerificationAndEncryption(Uri,IPackageInstallObserver, int, String, VerificationParams, ContainerEncryptionParams)方法:
代碼在PackageManager.java 3660行
/**
* Similar to
* {@link #installPackage(Uri, IPackageInstallObserver, int, String)} but
* with an extra verification information provided.
*
* @param packageURI The location of the package file to install. This can
* be a 'file:' or a 'content:' URI.
* @param observer An observer callback to get notified when the package
* installation is complete.
* {@link IPackageInstallObserver#packageInstalled(String, int)}
* will be called when that happens. This parameter must not be
* null.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING},
* {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that
* is performing the installation. This identifies which market
* the package came from.
* @param verificationParams an object that holds signal information to
* assist verification. May be {@code null}.
* @param encryptionParams if the package to be installed is encrypted,
* these parameters describing the encryption and authentication
* used. May be {@code null}.
* @hide
* @deprecated Use {@link #installPackageWithVerificationAndEncryption(Uri,
* PackageInstallObserver, int, String, VerificationParams,
* ContainerEncryptionParams)} instead. This method will
* continue to be supported but the older observer interface
* will not get additional failure details.
*/
@Deprecated
public abstract void installPackageWithVerificationAndEncryption(Uri packageURI,
IPackageInstallObserver observer, int flags, String installerPackageName,
VerificationParams verificationParams,
ContainerEncryptionParams encryptionParams);
通過代碼我們發(fā)現(xiàn)它是一個(gè) 系統(tǒng)API(SystemApi)
翻譯注釋如下:
- 和installPackage(Uri,IPackageInstallObserver,int,String)方法類似,就是比它多了一個(gè)額外的文件驗(yàn)證功能
- 入?yún)?packageURI :表示安裝的路徑,可以是"file:"或者"content:"的URI
- 入?yún)?observer :一個(gè)回調(diào)的觀察者,有了這個(gè)觀察者,就可以在軟件包安裝完成后得到安裝結(jié)果的通知。如果安裝完成會調(diào)用這個(gè)觀察者IPackageInstallObserver的packageInstalled(String,int)方法。observer這個(gè)入?yún)⒉荒転榭铡?/li>
- 入?yún)?flags :標(biāo)志位參數(shù),可能是以下的幾個(gè)值
- INSTALL_FORWARD_LOCK:安裝時(shí)候的標(biāo)志位,表示應(yīng)用程序?yàn)橄蚯版i定,即僅應(yīng)用程序本身可以訪問其代碼和非資源的assets
- INSTALL_REPLACE_EXISTING:安裝時(shí)候的標(biāo)志位,表示如果在設(shè)備存在同一個(gè)包名的安裝包,則你要替換已安裝的軟件包。
- INSTALL_ALLOW_TEST:安裝時(shí)候的標(biāo)志位,表示是否允許安裝測試包(在AndroidManifest里面設(shè)置了android:testOnly)
- 入?yún)?installerPackageName :正在進(jìn)行安裝的安裝包包名
- 入?yún)?verificationParams :持有驗(yàn)證信息的對象,可能是null。
- 入?yún)?encryptionParams :一個(gè)描述加密和認(rèn)證狀態(tài)的對象,這個(gè)入?yún)⒛転閚ull。
- 注意事項(xiàng):不推薦使用這個(gè)方法(@deprecated):
建議使用installPackageWithVerification(Uri,PackageInstallObserver, int, String, VerificationParams,ContainerEncryptionParams)這個(gè)方法,在后續(xù)版本將支持installPackageWithVerification(Uri,PackageInstallObserver, int, String, Uri, ManifestDigest,ContainerEncryptionParams)這個(gè)方法,因?yàn)槔习姹镜膐bserver無法獲得額外的故障細(xì)節(jié)。
4、 public abstract void installPackage(Uri,PackageInstallObserver,int, String)方法:
代碼在PackageManager.java 3688行
/**
* @hide
*
* Install a package. Since this may take a little while, the result will
* be posted back to the given observer. An installation will fail if the calling context
* lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
* package named in the package file's manifest is already installed, or if there's no space
* available on the device.
*
* @param packageURI The location of the package file to install. This can be a 'file:' or a
* 'content:' URI.
* @param observer An observer callback to get notified when the package installation is
* complete. {@link PackageInstallObserver#packageInstalled(String, Bundle, int)} will be
* called when that happens. This parameter must not be null.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that is performing the
* installation. This identifies which market the package came from.
*/
public abstract void installPackage(
Uri packageURI, PackageInstallObserver observer,
int flags, String installerPackageName);
翻譯注釋如下:
- 安裝一個(gè)安裝包的時(shí)候,需要經(jīng)過一定的時(shí)間之后才能把安裝的結(jié)果返回個(gè)觀察者。如果在安裝并調(diào)用Context的時(shí)候 在android.Manifest.permission缺少INSTALL_PACKAGES權(quán)限將會導(dǎo)致安裝失敗。如果設(shè)備上已經(jīng)安裝了這個(gè)同一個(gè)包名的應(yīng)用程序或者在設(shè)備已經(jīng)沒有了合適的空間都會導(dǎo)致安裝失敗。
- 入?yún)?packageURI :表示安裝的路徑,可以是"file:"或者"content:"的URI
- 入?yún)?observer :一個(gè)回調(diào)的觀察者,有了這個(gè)觀察者,就可以在軟件包安裝完成后得到安裝結(jié)果的通知。如果安裝完成會調(diào)用這個(gè)觀察者IPackageInstallObserver的packageInstalled(String,int)方法。observer這個(gè)入?yún)⒉荒転榭铡?/li>
- 入?yún)?flags :標(biāo)志位參數(shù),可能是以下的幾個(gè)值
- INSTALL_FORWARD_LOCK:安裝時(shí)候的標(biāo)志位,表示應(yīng)用程序?yàn)橄蚯版i定,即僅應(yīng)用程序本身可以訪問其代碼和非資源的assets
- INSTALL_REPLACE_EXISTING:安裝時(shí)候的標(biāo)志位,表示如果在設(shè)備存在同一個(gè)包名的安裝包,則你要替換已安裝的軟件包。
- INSTALL_ALLOW_TEST:安裝時(shí)候的標(biāo)志位,表示是否允許安裝測試包(在AndroidManifest里面設(shè)置了android:testOnly)
- 入?yún)?installerPackageName :正在進(jìn)行安裝的安裝包包名
5、public abstract void installPackageWithVerification(Uri,PackageInstallObserver, int, String, Uri, ManifestDigest,ContainerEncryptionParams)方法:
代碼在PackageManager.java 3717行
/**
* Similar to
* {@link #installPackage(Uri, IPackageInstallObserver, int, String)} but
* with an extra verification file provided.
*
* @param packageURI The location of the package file to install. This can
* be a 'file:' or a 'content:' URI.
* @param observer An observer callback to get notified when the package installation is
* complete. {@link PackageInstallObserver#packageInstalled(String, Bundle, int)} will be
* called when that happens. This parameter must not be null.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that
* is performing the installation. This identifies which market
* the package came from.
* @param verificationURI The location of the supplementary verification
* file. This can be a 'file:' or a 'content:' URI. May be
* {@code null}.
* @param manifestDigest an object that holds the digest of the package
* which can be used to verify ownership. May be {@code null}.
* @param encryptionParams if the package to be installed is encrypted,
* these parameters describing the encryption and authentication
* used. May be {@code null}.
* @hide
*/
public abstract void installPackageWithVerification(Uri packageURI,
PackageInstallObserver observer, int flags, String installerPackageName,
Uri verificationURI, ManifestDigest manifestDigest,
ContainerEncryptionParams encryptionParams);
翻譯注釋如下:
- 和installPackage(Uri,IPackageInstallObserver,int,String)方法類似,就是比它多了一個(gè)額外的文件驗(yàn)證功能
- 入?yún)?packageURI :表示安裝的路徑,可以是"file:"或者"content:"的URI
- 入?yún)?observer :一個(gè)回調(diào)的觀察者,有了這個(gè)觀察者,就可以在軟件包安裝完成后得到安裝結(jié)果的通知。如果安裝完成會調(diào)用這個(gè)觀察者IPackageInstallObserver的packageInstalled(String,int)方法。observer這個(gè)入?yún)⒉荒転榭铡?/li>
- 入?yún)?flags :標(biāo)志位參數(shù),可能是以下的幾個(gè)值
- INSTALL_FORWARD_LOCK:安裝時(shí)候的標(biāo)志位,表示應(yīng)用程序?yàn)橄蚯版i定,即僅應(yīng)用程序本身可以訪問其代碼和非資源的assets
- INSTALL_REPLACE_EXISTING:安裝時(shí)候的標(biāo)志位,表示如果在設(shè)備存在同一個(gè)包名的安裝包,則你要替換已安裝的軟件包。
- INSTALL_ALLOW_TEST:安裝時(shí)候的標(biāo)志位,表示是否允許安裝測試包(在AndroidManifest里面設(shè)置了android:testOnly)
- 入?yún)?installerPackageName :正在進(jìn)行安裝的安裝包包名
- 入?yún)?verificationURI :驗(yàn)證文件的位置,可以是"file:"或者"content:"的URI,該入?yún)⒖赡転閚ull
- 入?yún)?manifestDigest :一個(gè)包含可用于驗(yàn)證所有權(quán)的包的摘要的對象,該入?yún)⒖赡転閚ull
- 入?yún)?encryptionParams :一個(gè)描述加密和認(rèn)證狀態(tài)的對象,這個(gè)入?yún)⒛転閚ull。
6、public abstract void installPackageWithVerificationAndEncryption(Uri,PackageInstallObserver, int, String,VerificationParams, ContainerEncryptionParams)方法:
代碼在PackageManager.java 3745行
/**
* Similar to
* {@link #installPackage(Uri, IPackageInstallObserver, int, String)} but
* with an extra verification information provided.
*
* @param packageURI The location of the package file to install. This can
* be a 'file:' or a 'content:' URI.
* @param observer An observer callback to get notified when the package installation is
* complete. {@link PackageInstallObserver#packageInstalled(String, Bundle, int)} will be
* called when that happens. This parameter must not be null.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that
* is performing the installation. This identifies which market
* the package came from.
* @param verificationParams an object that holds signal information to
* assist verification. May be {@code null}.
* @param encryptionParams if the package to be installed is encrypted,
* these parameters describing the encryption and authentication
* used. May be {@code null}.
*
* @hide
*/
public abstract void installPackageWithVerificationAndEncryption(Uri packageURI,
PackageInstallObserver observer, int flags, String installerPackageName,
VerificationParams verificationParams, ContainerEncryptionParams encryptionParams);
翻譯注釋如下:
- 和installPackage(Uri,IPackageInstallObserver,int,String)方法類似,就是比它多了一個(gè)額外的文件驗(yàn)證功能
- 入?yún)?packageURI :表示安裝的路徑,可以是"file:"或者"content:"的URI
- 入?yún)?observer :一個(gè)回調(diào)的觀察者,有了這個(gè)觀察者,就可以在軟件包安裝完成后得到安裝結(jié)果的通知。如果安裝完成會調(diào)用這個(gè)觀察者IPackageInstallObserver的packageInstalled(String,int)方法。observer這個(gè)入?yún)⒉荒転榭铡?/li>
- 入?yún)?flags :標(biāo)志位參數(shù),可能是以下的幾個(gè)值
- INSTALL_FORWARD_LOCK:安裝時(shí)候的標(biāo)志位,表示應(yīng)用程序?yàn)橄蚯版i定,即僅應(yīng)用程序本身可以訪問其代碼和非資源的assets
- INSTALL_REPLACE_EXISTING:安裝時(shí)候的標(biāo)志位,表示如果在設(shè)備存在同一個(gè)包名的安裝包,則你要替換已安裝的軟件包。
- INSTALL_ALLOW_TEST:安裝時(shí)候的標(biāo)志位,表示是否允許安裝測試包(在AndroidManifest里面設(shè)置了android:testOnly)
- 入?yún)?installerPackageName :正在進(jìn)行安裝的安裝包包名
- 入?yún)?verificationParams :持有驗(yàn)證信息的對象,可能是null。
- 入?yún)?encryptionParams :一個(gè)描述加密和認(rèn)證狀態(tài)的對象,這個(gè)入?yún)⒛転閚ull。
7、public abstract int installExistingPackage(String)方法:
代碼在PackageManager.java 3755行
/**
* If there is already an application with the given package name installed
* on the system for other users, also install it for the calling user.
* @hide
*/
// @SystemApi
public abstract int installExistingPackage(String packageName)
throws NameNotFoundException;
翻譯注釋如下:
- 如果系統(tǒng)上已經(jīng)安裝相同包名的應(yīng)用程序,則重復(fù)重新安裝。
上一篇文章 APK安裝流程詳解1——有關(guān)"安裝ing"的實(shí)體類概述-
下一篇文章 APK安裝流程詳解3——PackageManager與PackageManagerService