getPackageManager().hasSystemFeature(String name);
//例如
getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
getPackageManager().hasSystemFe(PackageManager.FEATURE_BLUETOOTH);
范例:
PackageManager pm = context.getPackageManager();
FeatureInfo[] features = pm.getSystemAvailableFeatures(); //得到所有支援的硬件种类
for (FeatureInfo feature : features) Log.v(TAG, feature.name);
boolean hasFlashlight = pm.hsaSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); //是否支援閃光燈
boolean hasCamera = pm.hsaSystemFeature(PackageManager.FEATURE_CAMERA); //是否支援相機
boolean hasBt = pm.hsaSystemFeature(PackageManager.FEATURE_BLUETOOTH); //是否支援藍牙
hasSystemFeature 這個接口參考信息是
/system/etc/permissions/android.hardware*.xml 文件中定義的feature的信息
/frameworks/base/data/etc/android.hardware.camera.flash-autofocus.xml
< ?xml version="1.0" encoding="utf-8"?>
<permissions>
<feature name="android.hardware.camera" />
<feature name="android.hardware.camera.autofocus" />
<feature name="android.hardware.camera.flash" />
</permissions>
如果想增加Feature配置,可以再framework/base/data/etc/目录下增加xml文件
-
获取当前设备支持的所有features
FeatureInfo[] f = getPackageManager().getSystemAvailableFeatures();
-
判断当前设备是否支持某项feature
boolean b = getPackageManager().hasSystemFeature(featureName);
-
获取当前设备上已经安装的其他app的uses-feature配置,获取其他app的uses-feature配置不需要特别的权限,设备上已安装的任意一个app都可以获取。
try { String packageName = getPackageName(); PackageInfo p = getPackageManager().getPackageInfo(packageName, PackageManager.GET_CONFIGURATIONS); FeatureInfo[] f = p.reqFeatures; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); }
网友评论