本文共 17058 字,大约阅读时间需要 56 分钟。
Android应用生死轮回的那些事儿(4) - 武器库(2)-应用组件查询相关API
这一节我们将学习:
首先我们先看一下应用组件查询相关API的分类:
我们先看其中第一类,静态get类的API
集合查询类API的特点是一次性可以查到符合条件的应用组件,如包或者应用程序的完整列表。
作用:
获取当前系统上安装的所有的应用程序。原型:
ListgetInstalledApplications (int flags);
参数还是那几个,上一节已经讲过了。
例程:
public void testGetInstalledApplications() { ListinstalledApplications = 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.DocumentsApplication08-12 14:06:20.961 724-724/? D/TestPackageManager: Application process name is:com.android.documentsui08-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.Application08-12 14:06:20.964 724-724/? D/TestPackageManager: Application process name is:com.tencent.mm...
作用:根据包名,获取相关的ApplicationInfo
原型:
ApplicationInfo getApplicationInfo (String packageName,int flags);
参数:flags
例程:
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.htmlviewer08-12 14:49:51.633 1497-1497/? D/TestPackageManager: Description is:null08-12 14:49:51.633 1497-1497/? D/TestPackageManager: Share Libraries in:/system/app/HTMLViewer/lib/arm64
作用:获取当前系统中安装的所有包
原型:
ListgetInstalledPackages (int flags)
参数:
取值如下,望文知义,就不多解释了:
例程:
public void testGetInstalledPackages() { ListpiList = 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.gba08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package shared user id is:android.uid.phone08-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.ims08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package shared user id is:android.uid.phone08-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.ppl08-12 14:27:25.228 1244-1244/? D/TestPackageManager: Package shared user id is:android.uid.system
功能:根据包名获取一个包的信息
原型:
PackageInfo getPackageInfo (String packageName, int flags);
参数:
例程:
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.externalstorage08-12 15:07:18.457 2044-2044/? D/TestPackageManager: Package shared user id is:null08-12 15:07:18.457 2044-2044/? D/TestPackageManager: Version name:6.0-1468998115
功能:从包文件中读取PackageInfo
参数:
上个函数是读已安装的活着的,这个是去读文件中的。
根据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() { ListpiList = 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); } } } } }
根据名字获取Instrumentation信息,这个信息在AndroidManifest.xml中的中定义
原型:
InstrumentationInfo getInstrumentationInfo (ComponentName className, int flags);
通过ComponentName获取ServiceInfo.
原型:
ServiceInfo getServiceInfo (ComponentName component, int flags);
根据ComponentName获取ReceiverInfo
原型:
ActivityInfo getReceiverInfo (ComponentName component, int flags);
有同学问了,既然可以查询ActivityInfo,ServiceInfo,ReceiverInfo,四大组件中都有三个了,那么为什么没有查询ProviderInfo的呢?
getProviderInfo这个API直到Android 2.3时才有原型:
ProviderInfo getProviderInfo (ComponentName component, int flags);
功能:读取和包名相关的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:102308-12 15:30:36.783 2329-2329/? D/TestPackageManager: gid:1015
参数: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.mediatek08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.thermalmanager08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.batterywarning08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.connectivity08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.server.telecom08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.inputdevices08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.settings08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:android08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.atci.service08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.providers.settings08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.vpndialogs08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.ppl08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.location.fused08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.nlpservice08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.mediatek.schpwronoff08-12 16:42:38.730 4736-4736/? D/TestPackageManager: Package name in 1000:com.android.keychain
功能:查询一个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:
功能:查询系统中运行的ContentProvider
原型:
ListqueryContentProviders (String processName,int uid,int flags)
参数:
例程:
public void testQueryContentProviders() { Listpis = 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}...
功能:根据包名查询Instrumentation
原型:
ListqueryInstrumentation (String targetPackage,int flags);
参数:
例程:
public void testQueryInstrumentation(){ Listlist = mPm.queryInstrumentation(null,PackageManager.GET_META_DATA); if(list!=null){ for(InstrumentationInfo ii : list){ Log.d(TAG,"Instrumentation info:"+ii.toString()); } } }
功能:根据Intent查询可以处理这个Intent的所有BroadcastReceivers
原型:
ListqueryBroadcastReceivers (Intent intent,int flags);
功能:查询可以处理这个Intent的所有Activity
原型:
ListqueryIntentActivities (Intent intent,int flags);
参数:
功能:跟queryIntentActivities基本一样,支持更多的查询条件。
原型:
ListqueryIntentActivityOptions (ComponentName caller, Intent[] specifics, Intent intent,int flags);
功能:查询可以处理这个Intent的所有Service
原型:
ListqueryIntentServices (Intent intent,int flags);
大家都知道,Android有四大组件:Activity, Service, BroadcastReceiver和ContentProvider。前面三个组件都可以通过Intent查询了,为什么没有queryIntentContentProviders呢?
queryIntentContentProviders直到Android 4.4才增加进来。功能:查询可以处理这个Intent的所有ContentProvider们。
原型:
ListqueryIntentContentProviders (Intent intent, int flags);
功能:查找最适合些Intent的Activity
原型:
ResolveInfo resolveActivity (Intent intent, int flags);
功能:查找最适合些Intent的ContentProvider
原型:
ResolveInfo resolveContentProvider (Intent intent, int flags);
功能:查找最适合些Intent的服务
原型:
ResolveInfo resolveService (Intent intent, int flags);
上面3个API都是拿到了Intent再去判断它最适合的应用组件。那么,我们想要做逆运算,知道一个包了,如何去得到启动它的Intent呢?
这个API直到Android 1.5(API 3)时才加入系统中: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 }
转载地址:http://aouml.baihongyu.com/