本文主要是介绍学习AOSP中的PackageManager例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
AOSP提供了PM的测试demo,可以较好地掌握跟PM相关的各种api。
cts\tests\tests\content\src\android\content\pm\cts\PackageManagerTest.java(Android 5.1)
package android.content.pm.cts;
import com.android.cts.content.R;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.InstrumentationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.test.AndroidTestCase;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*** This test is based on the declarations in AndroidManifest.xml. We create mock declarations* in AndroidManifest.xml just for test of PackageManager, and there are no corresponding parts* of these declarations in test project.*/
public class PackageManagerTest extends AndroidTestCase {private PackageManager mPackageManager;private static final String PACKAGE_NAME = "com.android.cts.content";private static final String CONTENT_PKG_NAME = "com.android.cts.content";private static final String ACTIVITY_ACTION_NAME = "android.intent.action.PMTEST";private static final String MAIN_ACTION_NAME = "android.intent.action.MAIN";private static final String SERVICE_ACTION_NAME ="android.content.pm.cts.activity.PMTEST_SERVICE";private static final String PERMISSION_NAME = "android.permission.INTERNET";private static final String ACTIVITY_NAME = "android.content.pm.cts.TestPmActivity";private static final String SERVICE_NAME = "android.content.pm.cts.TestPmService";private static final String RECEIVER_NAME = "android.content.pm.cts.PmTestReceiver";private static final String INSTRUMENT_NAME = "android.content.pm.cts.TestPmInstrumentation";private static final String PROVIDER_NAME = "android.content.cts.MockContentProvider";private static final String PERMISSIONGROUP_NAME = "android.permission-group.COST_MONEY";@Overrideprotected void setUp() throws Exception {super.setUp();mPackageManager = getContext().getPackageManager();}public void testQuery() throws NameNotFoundException {// Test query Intent Activity related methodsIntent activityIntent = new Intent(ACTIVITY_ACTION_NAME);String cmpActivityName = "android.content.pm.cts.TestPmCompare";// List with different activities and the filter doesn't work,List<ResolveInfo> listWithDiff = mPackageManager.queryIntentActivityOptions(new ComponentName(PACKAGE_NAME, cmpActivityName), null, activityIntent, 0);checkActivityInfoName(ACTIVITY_NAME, listWithDiff);// List with the same activities to make filter workList<ResolveInfo> listInSame = mPackageManager.queryIntentActivityOptions(new ComponentName(PACKAGE_NAME, ACTIVITY_NAME), null, activityIntent, 0);assertEquals(0, listInSame.size());// Test queryIntentActivitiesList<ResolveInfo> intentActivities =mPackageManager.queryIntentActivities(activityIntent, 0);assertTrue(intentActivities.size() > 0);checkActivityInfoName(ACTIVITY_NAME, intentActivities);// End of Test query Intent Activity related methods// Test queryInstrumentationString targetPackage = "android";List<InstrumentationInfo> instrumentations = mPackageManager.queryInstrumentation(targetPackage, 0);checkInstrumentationInfoName(INSTRUMENT_NAME, instrumentations);// Test queryIntentServicesIntent serviceIntent = new Intent(SERVICE_ACTION_NAME);List<ResolveInfo> services = mPackageManager.queryIntentServices(serviceIntent,PackageManager.GET_INTENT_FILTERS);checkServiceInfoName(SERVICE_NAME, services);// Test queryBroadcastReceiversString receiverActionName = "android.content.pm.cts.PackageManagerTest.PMTEST_RECEIVER";Intent broadcastIntent = new Intent(receiverActionName);List<ResolveInfo> broadcastReceivers = new ArrayList<ResolveInfo>();broadcastReceivers = mPackageManager.queryBroadcastReceivers(broadcastIntent, 0);checkActivityInfoName(RECEIVER_NAME, broadcastReceivers);// Test queryPermissionsByGroup, queryContentProvidersString testPermissionsGroup = "android.permission-group.NETWORK";List<PermissionInfo> permissions = mPackageManager.queryPermissionsByGroup(testPermissionsGroup, PackageManager.GET_META_DATA);checkPermissionInfoName(PERMISSION_NAME, permissions);ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0);List<ProviderInfo> providers = mPackageManager.queryContentProviders(PACKAGE_NAME,appInfo.uid, 0);checkProviderInfoName(PROVIDER_NAME, providers);}private void checkActivityInfoName(String expectedName, List<ResolveInfo> resolves) {// Flag for checking if the name is contained in list array.boolean isContained = false;Iterator<ResolveInfo> infoIterator = resolves.iterator();String current;while (infoIterator.hasNext()) {current = infoIterator.next().activityInfo.name;if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}private void checkServiceInfoName(String expectedName, List<ResolveInfo> resolves) {boolean isContained = false;Iterator<ResolveInfo> infoIterator = resolves.iterator();String current;while (infoIterator.hasNext()) {current = infoIterator.next().serviceInfo.name;if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}private void checkPermissionInfoName(String expectedName, List<PermissionInfo> permissions) {boolean isContained = false;Iterator<PermissionInfo> infoIterator = permissions.iterator();String current;while (infoIterator.hasNext()) {current = infoIterator.next().name;if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}private void checkProviderInfoName(String expectedName, List<ProviderInfo> providers) {boolean isContained = false;Iterator<ProviderInfo> infoIterator = providers.iterator();String current;while (infoIterator.hasNext()) {current = infoIterator.next().name;if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}private void checkInstrumentationInfoName(String expectedName,List<InstrumentationInfo> instrumentations) {boolean isContained = false;Iterator<InstrumentationInfo> infoIterator = instrumentations.iterator();String current;while (infoIterator.hasNext()) {current = infoIterator.next().name;if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}public void testGetInfo() throws NameNotFoundException {// Test getApplicationInfo, getTextApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0);int discriptionRes = R.string.hello_android;String expectedDisciptionRes = "Hello, Android!";CharSequence appText = mPackageManager.getText(PACKAGE_NAME, discriptionRes, appInfo);assertEquals(expectedDisciptionRes, appText);ComponentName activityName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME);ComponentName serviceName = new ComponentName(PACKAGE_NAME, SERVICE_NAME);ComponentName receiverName = new ComponentName(PACKAGE_NAME, RECEIVER_NAME);ComponentName instrName = new ComponentName(PACKAGE_NAME, INSTRUMENT_NAME);// Test getPackageInfoPackageInfo packageInfo = mPackageManager.getPackageInfo(PACKAGE_NAME,PackageManager.GET_INSTRUMENTATION);assertEquals(PACKAGE_NAME, packageInfo.packageName);// Test getApplicationInfo, getApplicationLabelString appLabel = "Android TestCase";assertEquals(appLabel, mPackageManager.getApplicationLabel(appInfo));assertEquals(PACKAGE_NAME, appInfo.processName);// Test getServiceInfoassertEquals(SERVICE_NAME, mPackageManager.getServiceInfo(serviceName,PackageManager.GET_META_DATA).name);// Test getReceiverInfoassertEquals(RECEIVER_NAME, mPackageManager.getReceiverInfo(receiverName, 0).name);// Test getPackageArchiveInfofinal String apkRoute = getContext().getPackageCodePath();final String apkName = getContext().getPackageName();assertEquals(apkName, mPackageManager.getPackageArchiveInfo(apkRoute, 0).packageName);// Test getPackagesForUid, getNameForUidcheckPackagesNameForUid(PACKAGE_NAME, mPackageManager.getPackagesForUid(appInfo.uid));assertEquals(PACKAGE_NAME, mPackageManager.getNameForUid(appInfo.uid));// Test getActivityInfoassertEquals(ACTIVITY_NAME, mPackageManager.getActivityInfo(activityName, 0).name);// Test getPackageGidsassertTrue(mPackageManager.getPackageGids(PACKAGE_NAME).length > 0);// Test getPermissionInfoassertEquals(PERMISSION_NAME, mPackageManager.getPermissionInfo(PERMISSION_NAME, 0).name);// Test getPermissionGroupInfoassertEquals(PERMISSIONGROUP_NAME, mPackageManager.getPermissionGroupInfo(PERMISSIONGROUP_NAME, 0).name);// Test getAllPermissionGroupsList<PermissionGroupInfo> permissionGroups = mPackageManager.getAllPermissionGroups(0);checkPermissionGroupInfoName(PERMISSIONGROUP_NAME, permissionGroups);// Test getInstalledApplicationsassertTrue(mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA).size() > 0);// Test getInstalledPacakgeassertTrue(mPackageManager.getInstalledPackages(0).size() > 0);// Test getInstrumentationInfoassertEquals(INSTRUMENT_NAME, mPackageManager.getInstrumentationInfo(instrName, 0).name);// Test getSystemSharedLibraryNames, in javadoc, String array and null// are all OK as return value.mPackageManager.getSystemSharedLibraryNames();// Test getLaunchIntentForPackage, Intent of activity// android.content.pm.cts.TestPmCompare is set to match the condition// to make sure the return of this method is not null.assertEquals(MAIN_ACTION_NAME, mPackageManager.getLaunchIntentForPackage(PACKAGE_NAME).getAction());// Test isSafeMode. Because the test case will not run in safe mode, so// the return will be false.assertFalse(mPackageManager.isSafeMode());}private void checkPackagesNameForUid(String expectedName, String[] uid) {boolean isContained = false;for (int i = 0; i < uid.length; i++) {if (uid[i].equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}private void checkPermissionGroupInfoName(String expectedName,List<PermissionGroupInfo> permissionGroups) {boolean isContained = false;Iterator<PermissionGroupInfo> infoIterator = permissionGroups.iterator();String current;while (infoIterator.hasNext()) {current = infoIterator.next().name;if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}/*** Simple test for {@link PackageManager#getPreferredActivities(List, List, String)} that tests* calling it has no effect. The method is essentially a no-op because no preferred activities* can be added.* @see PackageManager#addPreferredActivity(IntentFilter, int, ComponentName[], ComponentName)*/public void testGetPreferredActivities() {assertNoPreferredActivities();}/*** Helper method to test that {@link PackageManager#getPreferredActivities(List, List, String)}* returns empty lists.*/private void assertNoPreferredActivities() {List<ComponentName> outActivities = new ArrayList<ComponentName>();List<IntentFilter> outFilters = new ArrayList<IntentFilter>();mPackageManager.getPreferredActivities(outFilters, outActivities, PACKAGE_NAME);assertEquals(0, outActivities.size());assertEquals(0, outFilters.size());}/*** Test that calling {@link PackageManager#addPreferredActivity(IntentFilter, int,* ComponentName[], ComponentName)} throws a {@link SecurityException}.* <p/>* The method is protected by the {@link android.permission.SET_PREFERRED_APPLICATIONS}* signature permission. Even though this app declares that permission, it still should not be* able to call this method because it is not signed with the platform certificate.*/public void testAddPreferredActivity() {IntentFilter intentFilter = new IntentFilter(ACTIVITY_ACTION_NAME);ComponentName[] componentName = {new ComponentName(PACKAGE_NAME, ACTIVITY_NAME)};try {mPackageManager.addPreferredActivity(intentFilter, IntentFilter.MATCH_CATEGORY_HOST,componentName, componentName[0]);fail("addPreferredActivity unexpectedly succeeded");} catch (SecurityException e) {// expected}assertNoPreferredActivities();}/*** Test that calling {@link PackageManager#clearPackagePreferredActivities(String)} has no* effect.*/public void testClearPackagePreferredActivities() {// just ensure no unexpected exceptions are thrown, nothing else to domPackageManager.clearPackagePreferredActivities(PACKAGE_NAME);}private void checkComponentName(String expectedName, List<ComponentName> componentNames) {boolean isContained = false;Iterator<ComponentName> nameIterator = componentNames.iterator();String current;while (nameIterator.hasNext()) {current = nameIterator.next().getClassName();if (current.equals(expectedName)) {isContained = true;break;}}assertTrue(isContained);}private void checkIntentFilterAction(String expectedName, List<IntentFilter> intentFilters) {boolean isContained = false;Iterator<IntentFilter> filterIterator = intentFilters.iterator();IntentFilter currentFilter;String currentAction;while (filterIterator.hasNext()) {currentFilter = filterIterator.next();for (int i = 0; i < currentFilter.countActions(); i++) {currentAction = currentFilter.getAction(i);if (currentAction.equals(expectedName)) {isContained = true;break;}}}assertTrue(isContained);}public void testAccessEnabledSetting() {mPackageManager.setApplicationEnabledSetting(PACKAGE_NAME,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);assertEquals(PackageManager.COMPONENT_ENABLED_STATE_ENABLED,mPackageManager.getApplicationEnabledSetting(PACKAGE_NAME));ComponentName componentName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME);mPackageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);assertEquals(PackageManager.COMPONENT_ENABLED_STATE_ENABLED,mPackageManager.getComponentEnabledSetting(componentName));}public void testOpPermission() {PermissionInfo permissionInfo = new PermissionInfo();String permissionName = "com.android.cts.content.permission.TEST_DYNAMIC.ADD";permissionInfo.name = permissionName;permissionInfo.labelRes = R.string.permlab_testDynamic;permissionInfo.nonLocalizedLabel = "Test Tree";// TODO: Bug ID 1561181.// Can't add permission in dynamic way}public void testGetIcon() throws NameNotFoundException {assertNotNull(mPackageManager.getApplicationIcon(PACKAGE_NAME));assertNotNull(mPackageManager.getApplicationIcon(mPackageManager.getApplicationInfo(PACKAGE_NAME, 0)));assertNotNull(mPackageManager.getActivityIcon(new ComponentName(PACKAGE_NAME, ACTIVITY_NAME)));assertNotNull(mPackageManager.getActivityIcon(new Intent(MAIN_ACTION_NAME)));assertNotNull(mPackageManager.getDefaultActivityIcon());// getDrawable is called by ComponentInfo.loadIcon() which called by getActivityIcon()// method of PackageMaganer. Here is just assurance for its functionality.int iconRes = R.drawable.start;ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0);assertNotNull(mPackageManager.getDrawable(PACKAGE_NAME, iconRes, appInfo));}public void testCheckMethods() {assertEquals(PackageManager.SIGNATURE_MATCH, mPackageManager.checkSignatures(PACKAGE_NAME,CONTENT_PKG_NAME));assertEquals(PackageManager.PERMISSION_GRANTED,mPackageManager.checkPermission(PERMISSION_NAME, PACKAGE_NAME));}public void testResolveMethods() {// Test resolveActivityIntent intent = new Intent(ACTIVITY_ACTION_NAME);intent.setComponent(new ComponentName(PACKAGE_NAME, ACTIVITY_NAME));assertEquals(ACTIVITY_NAME, mPackageManager.resolveActivity(intent,PackageManager.MATCH_DEFAULT_ONLY).activityInfo.name);// Test resolveServiceintent = new Intent(SERVICE_ACTION_NAME);intent.setComponent(new ComponentName(PACKAGE_NAME, SERVICE_NAME));ResolveInfo resolveInfo = mPackageManager.resolveService(intent,PackageManager.GET_INTENT_FILTERS);assertEquals(SERVICE_NAME, resolveInfo.serviceInfo.name);// Test resolveContentProviderString providerAuthorities = "ctstest";assertEquals(PROVIDER_NAME,mPackageManager.resolveContentProvider(providerAuthorities, 0).name);}public void testGetResources() throws NameNotFoundException {ComponentName componentName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME);int resourceId = R.xml.pm_test;String xmlName = "com.android.cts.content:xml/pm_test";ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0);assertNotNull(mPackageManager.getXml(PACKAGE_NAME, resourceId, appInfo));assertEquals(xmlName, mPackageManager.getResourcesForActivity(componentName).getResourceName(resourceId));assertEquals(xmlName, mPackageManager.getResourcesForApplication(appInfo).getResourceName(resourceId));assertEquals(xmlName, mPackageManager.getResourcesForApplication(PACKAGE_NAME).getResourceName(resourceId));}public void testGetPackageArchiveInfo() throws Exception {final String apkPath = getContext().getPackageCodePath();final String apkName = getContext().getPackageName();final int flags = PackageManager.GET_SIGNATURES;final PackageInfo pkgInfo = mPackageManager.getPackageArchiveInfo(apkPath, flags);assertEquals("getPackageArchiveInfo should return the correct package name",apkName, pkgInfo.packageName);assertNotNull("Signatures should have been collected when GET_SIGNATURES flag specified",pkgInfo.signatures);}
}
API总结:
资源相关API
这些API虽然数量众多,但是功能相对简单,都是根据包头或者是ApplicationInfo或Activity信息去获取一些资源,如图标,文本等。
Android 1.0创始API
- getXml:获取xml
- getText:从资源中读取文本
- getResourcesForApplication(app):获取资源对象
- getResourcesForApplication(appPackageName):获取资源对象
- getResourcesForActivity:从Activity中获取资源对象
- getDrawable:获取Drawable
- getDefaultActivityIcon:Activity的默认Icon
- getApplicationLabel:获取Applicaiton的标签
- getApplicationIcon:获取图标
- getActivityIcon:获取图标
Android 2.3新增API
Android 2.3新增两个API,都是跟Logo相关。
- getApplicationLogo
- getActivityLogo
Android 4.4手表版新增API
手表上新增了Banner.
- getApplicationBanner
- getActivityBanner
Android 5.0新增API
Android 5.0新增了UserBadged UI,所以新增了三个方法:
- getUserBadgedLabel
- getUserBadgedIcon
- getUserBadgedDrawableForDensity
禁掉或者解禁相关API
Android中可以支持将应用或者组件禁掉或者解禁。
ComponentEnableSetting是通过组件名来设置,ApplicationEnableSetting是通过包名。
- setComponentEnabledSetting
- getComponentEnabledSetting
- setApplicationEnabledSetting
- getApplicationEnabledSetting
setApplicationEnabledSetting
原型:
void setApplicationEnabledSetting (String packageName, int newState, int flags)
参数:
- 包名
- 新状态:状态请见下面的3种。
- 标志位:要么是DONT_KILL_APP,要么是0.
状态可以为:
- COMPONENT_ENABLED_STATE_ENABLED
- COMPONENT_ENABLED_STATE_DISABLED
- COMPONENT_ENABLED_STATE_DEFAULT
例程:禁掉微信:
public void testSetApplicationEnableSetting() {mPm.setApplicationEnabledSetting("com.tencent.mm", PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);}
setComponentEnabledSetting
将包中的四大组件之一禁掉或解禁。
原型:
void setComponentEnabledSetting (ComponentName componentName, int newState, int flags);
参数与上面的针对整个应用的一致。
检查签名
Android 1.0时,只能检查两个包名的签名是否一致。
原型:
int checkSignatures (String pkg1, String pkg2);
到了Android 2.0,也可以支持对两个uid之间判断签名。
问题是,从包名获取uid的API,要到Android 7.0才有呢。。。
原型:
int checkSignatures (int uid1, int uid2);
Prefer相关
addPackageToPreferred API已经在Android 2.1被除名,所以Prefer相关的API,只剩下下面三个:
- getPreferredPackages
- getPreferredActivities
- clearPackagePreferredActivities
至此,Android 1.0的PackageManager提供的46个API全部讲完了。
feature相关API
Android 2.0开始引入feature的概念。我们可以通过getSystemAvailableFeatures方法获取系统支持哪些feature. 或者是通过hasSystemFeature(name)方法判断是否支持一个feature.
不过,Android 2.0的时候,feature是不分版本号的。支持版本号的或者是通过hasSystemFeature方法要到Android 7.0才会出品。
FeatureInfo类
属性:
- flags: FLAG_REQUIRED或0
- name:属性名
- reqGlEsVersion:在name为空时,为需要的OpenGL ES的版本号
- version:(Android 7.0才有)版本号
方法:
- getGlEsVersion(): 0x00010002代表1.2
getSystemAvailableFeatures
原型:
FeatureInfo[] getSystemAvailableFeatures ();
例程:
public void testGetSystemAvailableFeatures(){final FeatureInfo[] fis = mPm.getSystemAvailableFeatures();if(fis!=null){for(FeatureInfo fi:fis){Log.d(TAG,"Feature Info:"+fi.toString());}}}
输出结果:
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{385e67d android.hardware.sensor.proximity fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{deb6972 android.hardware.sensor.accelerometer fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{788d1c3 android.hardware.faketouch fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{4fcea40 android.hardware.usb.accessory fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{c8f6d79 android.software.backup fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{102aabe android.hardware.touchscreen fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{b1cc71f android.hardware.touchscreen.multitouch fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{e66a6c android.software.print fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{2106835 android.software.voice_recognizers fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{ebab4ca android.hardware.sensor.gyroscope fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{6d0b63b android.hardware.bluetooth fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{b05c158 android.hardware.camera.autofocus fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{46c52b1 android.hardware.telephony.gsm fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{9375396 android.hardware.usb.host fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{40a7b17 android.hardware.audio.output fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{4c19b04 android.hardware.camera.flash fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{cc268ed android.hardware.camera.front fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{f0d1322 android.hardware.screen.portrait fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{2b1b1b3 android.software.home_screen fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{d3f6370 android.hardware.microphone fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{ce9a6e9 android.hardware.bluetooth_le fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{18d3f6e android.hardware.touchscreen.multitouch.jazzhand fl=0x0}
08-17 11:40:49.889 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{f8bb60f android.software.app_widgets fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{e8f469c android.software.input_methods fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{32cc8a5 android.hardware.sensor.light fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{3d1e47a android.software.device_admin fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{457a42b android.hardware.camera fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{1983088 android.hardware.screen.landscape fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{dc64a21 android.software.managed_users fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{e89ce46 android.software.webview fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{daa5807 android.hardware.camera.any fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{4c3cd34 android.software.connectionservice fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{a1c675d android.hardware.touchscreen.multitouch.distinct fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{48488d2 android.hardware.location.network fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{28a6da3 android.hardware.wifi.direct fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{16a88a0 android.software.live_wallpaper fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{bbd1c59 android.hardware.location.gps fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{3fe601e android.hardware.wifi fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{9cc40ff android.hardware.location fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{bff8ecc android.hardware.telephony fl=0x0}
08-17 11:40:49.890 17111-17111/com.yunos.xulun.testcppjni2 D/TestPackageManager: Feature Info:FeatureInfo{71a2515 glEsVers=3.0 fl=0x0}
上面都是feature,最后一条是openGLES的版本号:3.0
hasSystemFeature
原型:
boolean hasSystemFeature (String name);
feature的取值可以是下面的字符串:
* API 7 (Android 2.1)开始支持的feature
* FEATURE_CAMERA
* FEATURE_CAMERA_AUTOFOCUS
* FEATURE_CAMERA_FLASH
* FEATURE_LIVE_WALLPAPER
* FEATURE_SENSOR_LIGHT
* FEATURE_SENSOR_PROXIMITY
* FEATURE_TELEPHONY
* FEATURE_TELEPHONY_CDMA
* FEATURE_TOUCHSCREEN_MULTITOUCH
* API 8 (Android 2.2)开始支持的feature
* FEATURE_BLUETOOTH
* FEATURE_LOCATION
* FEATURE_LOCATION_GPS
* FEATURE_LOCATION_NETWORK
* FEATURE_MICROPHONE
* FEATURE_SENSOR_ACCELEROMETER
* FEATURE_SENSOR_COMPASS
* FEATURE_TOUCHSCREEN
* FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT
* FEATURE_WIFI
* API 9 (Android 2.3)
* FEATURE_AUDIO_LOW_LATENCY
* FEATURE_CAMERA_FRONT
* FEATURE_NFC
* FEATURE_SENSOR_BAROMETER
* FEATURE_SENSOR_GYROSCOPE
* FEATURE_SIP
* FEATURE_SIP_VOIP
* FEATURE_TOUCHSCREEN_MULTITOUCH_JAZZHAND
* API 11 (Android 3.0)
* FEATURE_FAKETOUCH
* API 12 (Android 3.1)
* FEATURE_USB_ACCESSORY
* FEATURE_USB_HOST
* API 13 (Android 3.2)
* FEATURE_FAKETOUCH_MULTITOUCH_DISTINCT
* FEATURE_FAKETOUCH_MULTITOUCH_JAZZHAND
* FEATURE_SCREEN_LANDSCAPE
* FEATURE_SCREEN_PORTRAIT
* API 14 (Android 4.0)
* FEATURE_WIFI_DIRECT
* API 17 (Android 4.2)
* FEATURE_CAMERA_ANY
* API 18 (Android 4.3)
* FEATURE_APP_WIDGETS
* FEATURE_BLUETOOTH_LE
* FEATURE_HOME_SCREEN
* FEATURE_INPUT_METHODS
* API 19 (Android 4.4)
* FEATURE_CONSUMER_IR
* FEATURE_DEVICE_ADMIN
* FEATURE_NFC_HOST_CARD_EMULATION
* FEATURE_SENSOR_STEP_COUNTER
* FEATURE_SENSOR_STEP_DETECTOR
* API 20 (Android 4.4 Watch)
* FEATURE_BACKUP
* FEATURE_CAMERA_EXTERNAL
* FEATURE_PRINTING
* FEATURE_SENSOR_HEART_RATE
* FEATURE_WATCH
* FEATURE_WEBVIEW
* API 21 (Android 5.0)
* FEATURE_AUDIO_OUTPUT
* FEATURE_CAMERA_CAPABILITY_MANUAL_POST_PROCESSING
* FEATURE_CAMERA_CAPABILITY_MANUAL_SENSOR
* FEATURE_CAMERA_CAPABILITY_RAW
* FEATURE_CAMERA_LEVEL_FULL
* FEATURE_CONNECTION_SERVICE
* FEATURE_GAMEPAD
* FEATURE_LEANBACK
* FEATURE_LIVE_TV
* FEATURE_MANAGED_USERS
* FEATURE_OPENGLES_EXTENSION_PACK
* FEATURE_SECURELY_REMOVES_USERS
* FEATURE_SENSOR_AMBIENT_TEMPERATURE
* FEATURE_SENSOR_HEART_RATE_ECG
* FEATURE_SENSOR_RELATIVE_HUMIDITY
* FEATURE_VERIFIED_BOOT
* API 23 (Android 6.0)
* FEATURE_AUDIO_PRO
* FEATURE_AUTOMOTIVE
* FEATURE_FINGERPRINT
* FEATURE_HIFI_SENSORS
* FEATURE_MIDI
* API 24 (Android 7.0)
* FEATURE_ETHERNET
* FEATURE_FREEFORM_WINDOW_MANAGEMENT
* FEATURE_NFC_HOST_CARD_EMULATION_NFCF
* FEATURE_PICTURE_IN_PICTURE
* FEATURE_VR_MODE
* FEATURE_VR_MODE_HIGH_PERFORMANCE
* FEATURE_VULKAN_HARDWARE_LEVEL
* FEATURE_VULKAN_HARDWARE_VERSION
包名转换API - Android 2.2新增
原型:
String[] currentToCanonicalPackageNames (String[] names);
String[] canonicalToCurrentPackageNames (String[] names)
例程:
public void testCurrentToCanonicalPackageNames(){String[] canonicalNames = mPm.currentToCanonicalPackageNames(new String[]{"com.yunos.xulun.testcppjni2","com.tencent.mm","com.taobao.taobao"});if(canonicalNames!=null) {for (String name : canonicalNames) {Log.d(TAG, "CanonicalName is:"+name);}}}
Android 1.0 PackageManager API总结
我们小结一下Android 1.0的46个PackageManager API:
- 权限相关
- removePermission
- queryPermissionsByGroup
- getPermissionInfo
- getPermissionGroupInfo
- getAllPermissionGroups
- checkPermission
- addPermission
- 禁掉组件相关
- setComponentEnabledSetting
- getComponentEnabledSetting
- setApplicationEnabledSetting
- getApplicationEnabledSetting
- 最佳匹配组件
- resolveService
- resolveContentProvider
- resolveActivity
- query组件
- queryIntentServices
- queryIntentActivityOptions
- queryIntentActivities
- queryBroadcastReceivers
- queryInstrumentation
- queryContentProviders
- uid/gid相关
- getPackagesForUid
- getNameForUid
- getPackageGids(packageName)
- get组件集合
- getInstalledPackages
- getInstalledApplications
- get单个组件
- getPackageArchiveInfo
- getInstrumentationInfo
- getApplicationInfo
- getActivityInfo
- getPackageInfo
- getServiceInfo
- getReceiverInfo
- 获取资源
- getXml
- getText
- getResourcesForApplication(app)
- getResourcesForApplication(appPackageName)
- getResourcesForActivity
- getDrawable
- getDefaultActivityIcon
- getApplicationLabel
- getApplicationIcon
- getActivityIcon
- 最爱组件相关
- getPreferredPackages
- getPreferredActivities
- clearPackagePreferredActivities
- 比较签名
- checkSignatures(pkg,pkg)
Android 1.5新增Package API总结
API 3 (Android 1.5)新增了3个API:
- getLaunchIntentForPackage
- isSafeMode
- getSystemSharedLibraryNames
isSafeMode
是否处于安全模式。
getSystemSharedLibraryNames
返回so库的路径
Android 2.0新增API总结
API 5 (Android 2.0)新增4个API,前面已经都讲过了。
- getInstallerPackageName
- checkSignatures(uid1,uid2)
- hasSystemFeature(name)
- getSystemAvailableFeatures
Android 2.2新增API总结
API 8 (Android 2.2)新增3个API,2个名义包名转换的,还有一个权限管理的,前面都已经讲过了。
- currentToCanonicalPackageNames
- canonicalToCurrentPackageNames
- addPermissionAsync
Android 2.3新增API总结
API 9 (Android 2.3)新增3个API,一个是getProviderInfo,终于把四大组件的getAPI凑齐了。另外从本版开始有了Logo的概念。
- getProviderInfo
- getApplicationLogo
- getActivityLogo
Android 3.0新增API总结
API 11 (Android 3.0)只增加一个API,为2.0的getInstallerPackageName提供一个setter接口。
- setInstallerPackageName
Android 4.0新增API总结
API 14 (Android 4.0)也只增加一个API,用于安装时校验。
- verifyPendingInstall
Android 4.2新增API总结
API 17 (Android 4.2)只增加一个API,其实是对于上一个API的补足。
- extendVerificationTimeout
Android 4.3新增API总结
API 18 (Android 4.3)新增一个权限相关API
- getPackagesHoldingPermissions
Android 4.4新增API总结
API 19 (Android 4.4)只新增一个API,终于可以通过Intent查询ContentProvider了
- queryIntentContentProviders
Android 4.4手表版新增API总结
API 20 (KITKAT_WATCH)新增两个Banner相关API.
- getApplicationBanner
- getActivityBanner
Android 5.0新增API总结
API 21 (Android 5.0)新增5个API,前三个是UserBadged界面相关。
第4个提供了PackageInstaller的接口。
最后一个是Leanback界面下获取Launch intent的API
- getUserBadgedLabel
- getUserBadgedIcon
- getUserBadgedDrawableForDensity
- getPackageInstaller
- getLeanbackLaunchIntentForPackage
Android 6.0新增API总结
API 23 (Android 6.0)只增加一个权限相关API
- isPermissionRevokedByPolicy
Android 7.0新增API总结
API 24 (Android 7.0)新增3个API。
systemm feature开始支持版本号。
终于可以获取package的uid了。
- hasSystemFeature(name,version)
- getPackageUid
- getPackageGids(name,flags)
这篇关于学习AOSP中的PackageManager例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!