当前位置:   article > 正文

2022-04-21 Android 使用getPackageManager().hasSystemFeature方法来检查当前设备硬件是否支持某些功能

hassystemfeature

一、android frameworks里面经常会看到getPackageManager().hasSystemFeature,比如frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\policy\FlashlightControllerImpl.java里面检查硬件是否闪光灯。

二、getPackageManager().hasSystemFeature是用来检查android设备是否支持某些功能
我们在开发APP的时候,应用程序可能需要设备支持某些功能才能保证应用程序的运行。例如需要支持电话、陀螺仪等等。我们可以使用PackageManager对象的hasSystemFeature方法来检查当前设备是否支持某些功能。

三、分析一下AvailableFeature从哪里获取的

     1、frameworks\base\core\java\android\app\ApplicationPackageManager.java

  2、frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.java

 3、frameworks\base\core\java\com\android\server\SystemConfig.java里去获取支持的硬件设备,红框里面读取/vendor/etc/permissions/下面的xml文件。

 4、/vendor/etc/permissions/下面的文件,支持camera的xml文件的方式如下。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright (C) 2009 The Android Open Source Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. -->
  13. <!-- This is the standard feature indicating that the device includes WiFi. -->
  14. <permissions>
  15. <feature name="android.hardware.wifi" />
  16. </permissions>

 四、这里我自定义一个giadahdmicec硬件功能。

      1、app里面检查是否支持giadahdmicec功能

 2、把android.hardware.giada.xml文件adb push到/vendor/etc/permissions/目录下面

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright (C) 2009 The Android Open Source Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. -->
  13. <!-- This is the standard set of features for a camera with a flash. Note
  14. that this currently requires having auto-focus as well. -->
  15. <permissions>
  16. <feature name="android.hardware.giadahdmicec" />
  17. </permissions>

 3、运行app,说明该设备support cec功能。

 4、在AndroidManifest.xml文件中使用<uses-feature/>标签,来告诉Android Market此应用程序必须满足标签中指定功能才可以使用,这样Android Market会根据应用程序的指定的要求,来过滤所有不支持的设备。

五、参考文章

检查android设备是否支持某些功能 - 仰望 星空 - 博客园

PackageManager hasSystemFeature_a281629221的博客-CSDN博客_android hassystemfeature

android-getPackageManager().hasSystemFeature(String string)的应用_zJianFlys的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/545179
推荐阅读
相关标签
  

闽ICP备14008679号