美文网首页Android开发Android技术知识手机移动程序开发
hasSystemFeature()判断是否有特定的功能模块

hasSystemFeature()判断是否有特定的功能模块

作者: 97690CE50CC872D | 来源:发表于2017-08-30 19:11 被阅读414次

    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文件

    1. 获取当前设备支持的所有features

      FeatureInfo[] f = getPackageManager().getSystemAvailableFeatures();

    2. 判断当前设备是否支持某项feature

      boolean b = getPackageManager().hasSystemFeature(featureName);

    3. 获取当前设备上已经安装的其他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();
           }

    相关文章

      网友评论

        本文标题:hasSystemFeature()判断是否有特定的功能模块

        本文链接:https://www.haomeiwen.com/subject/rimxjxtx.html