Android应用生死轮回的那些事儿(4) - 武器库(2)-应用组件查询相关API
这一节我们将学习:
- 如何查询系统中安装了哪些应用
- 如何获取一个包的信息
- 如何获取一个Application的信息
- 如何获取系统中安装的所有的ContentProvider的信息
- 如何获取一个uid中运行的所有的包
- 如何查询一个uid的进程名
- 如何通过一个广播Intent查询接受这个广播所有的BroadcastReceiver
- 得到一个Intent,如何寻找执行它的最佳的Activity,Service或ContentProvider
首先我们先看一下应用组件查询相关API的分类:
我们先看其中第一类,静态get类的API
应用组件集合查询API
集合查询类API的特点是一次性可以查到符合条件的应用组件,如包或者应用程序的完整列表。
getInstalledApplications
作用:
获取当前系统上安装的所有的应用程序。
原型:
List<ApplicationInfo> getInstalledApplications (int flags);
参数还是那几个,上一节已经讲过了。
- GET_META_DATA:
- GET_SHARED_LIBRARY_FILES
- MATCH_SYSTEM_ONLY
- MATCH_UNINSTALLED_PACKAGES
例程:
public void testGetInstalledApplications() {
List<ApplicationInfo> installedApplications = mPm.getInstalledApplications(PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES);
if(installedApplications != null){
for(ApplicationInfo ai : installedApplications){
Log.d(TAG,"ApplicationInfo:"+ai.toString());
Log.d(TAG,"Application class name is:"+ai.className);
Log.d(TAG,"Application process name is:"+ai.processName);
}
}
}
输出结果例
下面是DocumentsApplication和微信两个应用的结果:
...
08-12 14:06:20.961 724-724/? D/TestPackageManager: ApplicationInfo:ApplicationInfo{4011cb6 com.android.documentsui}
08-12 14:06:20.961 724-724/? D/TestPackageManager: Application class name is:com.android.documentsui.DocumentsApplication
08-12 14:06:20.961 724-724/? D/TestPackageManager: Application process name is:com.android.documentsui
08-12 14:06:20.964 724-724/? D/TestPackageManager: ApplicationInfo:ApplicationInfo{30211f9 com.tencent.mm}
08-12 14:06:20.964 724-724/? D/TestPackageManager: Application class name is:com.tencent.mm.app.Application
08-12 14:06:20.964 724-724/? D/TestPackageManager: Application process name is:com.tencent.mm
...
应用组件单个数据结构查询
getApplicationInfo
作用:根据包名,获取相关的ApplicationInfo
原型:
ApplicationInfo getApplicationInfo (String packageName,int flags);
参数:flags
- GET_META_DATA
- GET_SHARED_LIBRARY_FILES
例程:
public void testGetApplicationInfo() {
try {
ApplicationInfo ai = mPm.getApplicationInfo("com.android.htmlviewer",
PackageManager.GET_META_DATA | PackageManager.GET_SHARED_LIBRARY_FILES);
Log.d(TAG,"ProcessName is:"+ai.processName);
Log.d(TAG,"Description is:"+ai.loadDescription(mPm));
Log.d(TAG, "Share Libraries in:" + ai.nativeLibraryDir);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "testGetApplicationInfo error", e);
}
}
输出:
08-12 14:49:51.632 1497-1497/? D/TestPackageManager: ProcessName is:com.android.htmlviewer
08-12 14:49:51.633 1497-1497/? D/TestPackageManager: Description is:null
08-12 14:49:51.633 1497-1497/? D/TestPackageManager: Share Libraries in:/system/app/HTMLViewer/lib/arm64
getInstalledPackages
作用:获取当前系统中安装的所有包
原型:
List<PackageInfo> getInstalledPackages (int flags)
参数:
- flags:指定需要提供的字段
取值如下,望文知义,就不多解释了:
- PackageManager.GET_ACTIVITIES
- PackageManager.GET_CONFIGURATIONS
- PackageManager.GET_GIDS
- PackageManager.GET_INSTRUMENTATION
- PackageManager.GET_INTENT_FILTERS
- PackageManager.GET_META_DATA
- PackageManager.GET_PERMISSIONS
- PackageManager.GET_PROVIDERS
- PackageManager.GET_RECEIVERS
- PackageManager.GET_SERVICES
- PackageManager.GET_SHARED_LIBRARY_FILES
- PackageManager.GET_SIGNATURES
- PackageManager.GET_URI_PERMISSION_PATTERN
例程:
public void testGetInstalledPackages() {
List<PackageInfo> piList = mPm.getInstalledPackages(PackageManager.GET_ACTIVITIES |
PackageManager.GET_CONFIGURATIONS |
PackageManager.GET_GIDS |
PackageManager.GET_INSTRUMENTATION |
PackageManager.GET_INTENT_FILTERS |
PackageManager.GET_META_DATA |
PackageManager.GET_PERMISSIONS |
PackageManager.GET_PROVIDERS |
PackageManager.GET_RECEIVERS |
PackageManager.GET_SERVICES |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_SIGNATURES |
PackageManager.GET_URI_PERMISSION_PATTERNS);
if (piList != null) {
for(PackageInfo pi : piList){
Log.d(TAG,"PackageInfo:"+pi.toString());
Log.d(TAG,"Package Name is:"+pi.packageName);
Log.d(TAG,"Package shared user id is:"+pi.sharedUserId);
}
}
}
部分输出结果:
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: PackageInfo:PackageInfo{9bc4a05 com.mediatek.gba}
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package Name is:com.mediatek.gba
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package shared user id is:android.uid.phone
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: PackageInfo:PackageInfo{8973a5a com.mediatek.ims}
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package Name is:com.mediatek.ims
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package shared user id is:android.uid.phone
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: PackageInfo:PackageInfo{7c7748b com.mediatek.ppl}
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package Name is:com.mediatek.ppl
08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package shared user id is:android.uid.system
getPackageInfo
功能:根据包名获取一个包的信息
原型:
PackageInfo getPackageInfo (String packageName, int flags);
参数:
- 包名
- flgas:跟getInstalledPackages中一样
例程:
public void testGetPackageInfo() {
try {
PackageInfo pi = mPm.getPackageInfo("com.android.externalstorage", PackageManager.GET_ACTIVITIES |
PackageManager.GET_CONFIGURATIONS |
PackageManager.GET_GIDS |
PackageManager.GET_INSTRUMENTATION |
PackageManager.GET_INTENT_FILTERS |
PackageManager.GET_META_DATA |
PackageManager.GET_PERMISSIONS |
PackageManager.GET_PROVIDERS |
PackageManager.GET_RECEIVERS |
PackageManager.GET_SERVICES |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_SIGNATURES |
PackageManager.GET_URI_PERMISSION_PATTERNS);
Log.d(TAG, "PackageInfo:" + pi.toString());
Log.d(TAG, "Package Name is:" + pi.packageName);
Log.d(TAG, "Package shared user id is:" + pi.sharedUserId);
Log.d(TAG, "Version name:" + pi.versionName);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "testGetPackageInfo error", e);
}
}
输出:
08-12 15:07:18.457 2044-2044/? D/TestPackageManager: PackageInfo:PackageInfo{cda5ddc com.android.externalstorage}
08-12 15:07:18.457 2044-2044/? D/TestPackageManager: Package Name is:com.android.externalstorage
08-12 15:07:18.457 2044-2044/? D/TestPackageManager: Package shared user id is:null
08-12 15:07:18.457 2044-2044/? D/TestPackageManager: Version name:6.0-1468998115
getPackageArchiveInfo
功能:从包文件中读取PackageInfo
参数:
- 文件名
- flags:与上面的一样
上个函数是读已安装的活着的,这个是去读文件中的。
getActivityInfo
根据Activity的名称来获取ActivityInfo
原型:
ActivityInfo getActivityInfo (ComponentName component, int flags)
ComponentName是个什么类呢?其实就是包名和类名的组合:
ComponentName(String pkg, String cls);
//Create a new component identifier.
特别要注意,类名是要全路径名,不然会找不到的!
例程:
public void testGetActivityInfo(){
try {
final ActivityInfo ai = mPm.getActivityInfo(new ComponentName("com.android.htmlviewer","com.android.htmlviewer.HTMLViewerActivity"),
PackageManager.GET_META_DATA | PackageManager.MATCH_ALL);
Log.d(TAG,"ActivityInfo:"+ai.toString());
Log.d(TAG,"ActivityInfo,parentActivityName is:"+ai.parentActivityName);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG,"testGetActivityInfo error",e);
}
}
输出:
08-15 11:27:57.534 18124-18124/? D/TestPackageManager: ActivityInfo:ActivityInfo{5bb2ee5 com.android.htmlviewer.HTMLViewerActivity}
08-15 11:27:57.534 18124-18124/? D/TestPackageManager: ActivityInfo,parentActivityName is:null
那么,如何可以获取当前系统中都有哪些Activity呢?这可以通过获取系统中安装的所有的包后,遍历其ActivityInfo数组得到:
public void testGetInstalledPackages() {
List<PackageInfo> piList = mPm.getInstalledPackages(PackageManager.GET_ACTIVITIES |
PackageManager.GET_CONFIGURATIONS |
PackageManager.GET_GIDS |
PackageManager.GET_INSTRUMENTATION |
PackageManager.GET_INTENT_FILTERS |
PackageManager.GET_META_DATA |
PackageManager.GET_PERMISSIONS |
PackageManager.GET_PROVIDERS |
PackageManager.GET_RECEIVERS |
PackageManager.GET_SERVICES |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_SIGNATURES |
PackageManager.GET_URI_PERMISSION_PATTERNS);
if (piList != null) {
for (PackageInfo pi : piList) {
Log.d(TAG, "PackageInfo:" + pi.toString());
Log.d(TAG, "Package Name is:" + pi.packageName);
Log.d(TAG, "Package shared user id is:" + pi.sharedUserId);
final ActivityInfo[] ais = pi.activities;
if (ais != null) {
for (ActivityInfo ai : ais) {
Log.d(TAG, "Activity in package:{" + pi.packageName + "}:" + ai.name);
}
}
final InstrumentationInfo[] iis = pi.instrumentation;
if (iis != null) {
for (InstrumentationInfo is : iis) {
Log.d(TAG, "Instrumentation info" + is.toString());
}
}
final ProviderInfo[] pis = pi.providers;
if (pis != null) {
for (ProviderInfo pri : pis) {
Log.d(TAG, "Provider info:" + pri);
}
}
}
}
}
getInstrumenationInfo
根据名字获取Instrumentation信息,这个信息在AndroidManifest.xml中的中定义
原型:
InstrumentationInfo getInstrumentationInfo (ComponentName className, int flags);
getServiceInfo
通过ComponentName获取ServiceInfo.
原型:
ServiceInfo getServiceInfo (ComponentName component, int flags);
getReceiverInfo
根据ComponentName获取ReceiverInfo
原型:
ActivityInfo getReceiverInfo (ComponentName component, int flags);
有同学问了,既然可以查询ActivityInfo,ServiceInfo,ReceiverInfo,四大组件中都有三个了,那么为什么没有查询ProviderInfo的呢?
getProviderInfo这个API直到Android 2.3时才有
Android 2.3新增API - getProviderInfo
原型:
ProviderInfo getProviderInfo (ComponentName component, int flags);
Uid/Gid相关查询API
getPackageGids
功能:读取和包名相关的Group id
参数:包名
例程:
public void testGetPackageGids(){
try {
int[] gids = mPm.getPackageGids("com.android.externalstorage");
if(gids!=null){
for(int gid:gids){
Log.d(TAG,"gid:"+gid);
}
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG,"testGetPackageGids error",e);
}
}
输出:
08-12 15:30:36.783 2329-2329/? D/TestPackageManager: gid:1023
08-12 15:30:36.783 2329-2329/? D/TestPackageManager: gid:1015
getPackagesForUid
参数:uid
例程,我们查查uid是1000的都是些什么包?
public void testGetPackagesForUid(){
String[] packagesForUid = mPm.getPackagesForUid(1000);
if(packagesForUid!=null){
for(String package1 : packagesForUid){
Log.d(TAG, "Package name in 1000:"+package1);
}
}
}
输出如下,原来用了uid 1000的有这么多包!
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.thermalmanager
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.batterywarning
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.connectivity
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.server.telecom
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.inputdevices
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.settings
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:android
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.atci.service
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.providers.settings
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.vpndialogs
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.ppl
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.location.fused
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.nlpservice
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.schpwronoff
08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.keychain
getNameForUid
功能:查询一个uid对应的名字
原型:
String getNameForUid (int uid);
参数:uid
例程:
public void testGetNameForUid(){
String name = mPm.getNameForUid(1000);
Log.d(TAG,"Name for uid 1000 is:"+name);
}
输出
08-13 16:55:05.207 8899-8899/? D/TestPackageManager: Name for uid 1000 is:android.uid.system:1000
既然可以通过uid查询有哪些包,那么我们可不可以通过一个包名去获取uid呢?
这个API:getPackageUid,在Android 7.0才会加到系统中。
上面都是静态的get API,后面将是query和resolve API依次出场。我们先看动态查询的query系的API:
根据包名动态查询的API
queryContentProviders
功能:查询系统中运行的ContentProvider
原型:
List<ProviderInfo> queryContentProviders (String processName,int uid,int flags)
参数:
- 进程名:如果希望列出所有的,就为空
- uid:如果进程名不为空,需要指定uid
- 标志位:match的属性
例程:
public void testQueryContentProviders() {
List<ProviderInfo> pis = mPm.queryContentProviders(null, 0, PackageManager.MATCH_ALL);
if (pis != null) {
for (ProviderInfo pri : pis) {
Log.d(TAG, "Provider info:" + pri);
}
}
}
输出的前几项,完整的列表很长:
08-15 11:41:39.758 18259-18259/? D/TestPackageManager: Provider info:ContentProviderInfo{name=settings className=com.android.providers.settings.SettingsProvider}
08-15 11:41:39.758 18259-18259/? D/TestPackageManager: Provider info:ContentProviderInfo{name=com.tencent.mm.plugin.ext.NearBy className=com.tencent.mm.plugin.ext.provider.ExtControlProviderNearBy}
08-15 11:41:39.758 18259-18259/? D/TestPackageManager: Provider info:ContentProviderInfo{name=mwimsg className=com.android.providers.telephony.MwiProvider}
...
queryInstrumentation
功能:根据包名查询Instrumentation
原型:
List<InstrumentationInfo> queryInstrumentation (String targetPackage,int flags);
参数:
- 包名:如果是所有的包,则给null
- 标志:只支持GET_META_DATA这一个。
例程:
public void testQueryInstrumentation(){
List<InstrumentationInfo> list = mPm.queryInstrumentation(null,PackageManager.GET_META_DATA);
if(list!=null){
for(InstrumentationInfo ii : list){
Log.d(TAG,"Instrumentation info:"+ii.toString());
}
}
}
根据Intent查询相关组件的API
queryBroadcastReceivers
功能:根据Intent查询可以处理这个Intent的所有BroadcastReceivers
原型:
List<ResolveInfo> queryBroadcastReceivers (Intent intent,int flags);
queryIntentActivities
功能:查询可以处理这个Intent的所有Activity
原型:
List<ResolveInfo> queryIntentActivities (Intent intent,int flags);
参数:
- Intent
- 参数:MATCH_ALL等
queryIntentActivityOptions
功能:跟queryIntentActivities基本一样,支持更多的查询条件。
原型:
List<ResolveInfo> queryIntentActivityOptions (ComponentName caller, Intent[] specifics, Intent intent,int flags);
queryIntentServices
功能:查询可以处理这个Intent的所有Service
原型:
List<ResolveInfo> queryIntentServices (Intent intent,int flags);
大家都知道,Android有四大组件:Activity, Service, BroadcastReceiver和ContentProvider。前面三个组件都可以通过Intent查询了,为什么没有queryIntentContentProviders呢?
queryIntentContentProviders直到Android 4.4才增加进来。
Android 4.4新增API: queryIntentContentProviders
功能:查询可以处理这个Intent的所有ContentProvider们。
原型:
List<ResolveInfo> queryIntentContentProviders (Intent intent, int flags);
根据Intent查询最佳应用组件API
resolveActivity
功能:查找最适合些Intent的Activity
原型:
ResolveInfo resolveActivity (Intent intent, int flags);
resolveContentProvider
功能:查找最适合些Intent的ContentProvider
原型:
ResolveInfo resolveContentProvider (Intent intent, int flags);
resolveService
功能:查找最适合些Intent的服务
原型:
ResolveInfo resolveService (Intent intent, int flags);
上面3个API都是拿到了Intent再去判断它最适合的应用组件。那么,我们想要做逆运算,知道一个包了,如何去得到启动它的Intent呢?
这个API直到Android 1.5(API 3)时才加入系统中:getLaunchIntentForPackage
Android 1.5新增API - getLaunchIntentForPackage
原型:
Intent getLaunchIntentForPackage (String packageName);
例程:我们想获取微信的启动Intent:
public void testGetLaunchIntentForPackage() {
Intent intent = mPm.getLaunchIntentForPackage("com.tencent.mm");
if (intent != null) {
Log.d(TAG, "Intent for Launch this is:" + intent.toString());
}
}
运行结果如下,原来启动微信的界面是com.tencent.mm.ui.LauncherUI。
08-15 14:23:59.909 18954-18954/? D/TestPackageManager: Intent for Launch this is:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.tencent.mm cmp=com.tencent.mm/.ui.LauncherUI }