学习AOSP中的PackageManager例子

2024-05-07 05:32

本文主要是介绍学习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例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966452

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

线性代数|机器学习-P36在图中找聚类

文章目录 1. 常见图结构2. 谱聚类 感觉后面几节课的内容跨越太大,需要补充太多的知识点,教授讲得内容跨越较大,一般一节课的内容是书本上的一章节内容,所以看视频比较吃力,需要先预习课本内容后才能够很好的理解教授讲解的知识点。 1. 常见图结构 假设我们有如下图结构: Adjacency Matrix:行和列表示的是节点的位置,A[i,j]表示的第 i 个节点和第 j 个

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件