Android 系統hasSystemFeature 添加Feature方法

介紹
        應用程序或者系統框架中可以通過getPackageManager().hasSystemFeature(String string)判斷系統是否支持特定的模塊功能,而運行不同的代碼邏輯分支。比如可以通過getPackageManager().hasSystemFeature("android.hardware.bluetooth")判斷系統是否支持藍牙。當我們定製系統的時候,需要在Android系統需要對系統提供的Feature進行裁剪和自定義,那麼如何去進行裁剪和自定呢?這是本文要討論的主題。

Feature實現原理
        參考https://blog.csdn.net/u013398960/article/details/78582096

添加自定義Feature方法1


        從Feature實現原理知道,系統Feature定義在frameworks/native/data/etc目錄下,(MTK的不一定上此目錄,MTK的部分硬件功能定義 在alps\device\mediateksample\xprojectname\android.hardware.camera.xml,如此目錄中的ndroid.hardware.camera.xml文件會覆蓋frameworks/native/data/etc/android.hardware.camera.xml文件中的功能定義),這下面的xml文件會被打包拷貝到鏡像system/etc/permissions目錄下。所以要自定義Feature,最簡單的方法就是在frameworks/native/data/etc添加自定義的xml文件,xml文件內容格式參照:

<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2011 The Android Open Source Project

 

     Licensed under the Apache License, Version 2.0 (the "License");

     you may not use this file except in compliance with the License.

     You may obtain a copy of the License at

   

          http://www.apache.org/licenses/LICENSE-2.0

   

     Unless required by applicable law or agreed to in writing, software

     distributed under the License is distributed on an "AS IS" BASIS,

     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

     See the License for the specific language governing permissions and

     limitations under the License.

-->

<!-- Adds the feature indicating support for the Bluetooth API -->

<permissions>

    <feature name="android.hardware.bluetooth" />

</permissions>

添加自定義Feature方法2
       如果出於獨立管理或者解耦的原因,並不想在frameworks/native/data/etc目錄下添加文件,提供第二種自定義Feature的方法:

在aosp根目錄下新建自己的git倉庫目錄,在新建的倉庫目錄下添加自定義Feature的xml文件,格式和添加自定義Feature方法1中講的一樣;
在自定的倉庫目錄下新建device.mk,device.mk文件添加內容示例內容,編譯時將自定義feature文件打包到鏡像目錄中:

PRODUCT_COPY_FILES += mydir/com.new.feature.xml:system/etc/permissions/com.new.feature.xml

以高通爲例,在device/qcom/common/base.mk文件中加入#include "mydir/device.mk"文件;
重新編譯系統
————————————————
版權聲明:本文爲CSDN博主「_晴少_」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/wanhongluli/article/details/97303588

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章