Android framework開發全域系統屬性設定與擷取

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   java   

Android命令列/c語言/java設定擷取系統屬性的方法

Java中設定和擷取SystemProperty屬性:

SystemProperties.getInt("persist.sys.boot_music_control", 0)SystemProperties.set("persist.sys.boot_music_control", ""+value);



C++中擷取Java層中設定的屬性值:

 
           char boot_music_control[PROPERTY_VALUE_MAX];            if (property_get("persist.sys.boot_music_control", boot_music_control, 0) != 0) {                mediastatus = mediaplayer->setDataSource(pBackupSoundFileName, NULL);            }



    1 命令列(shell) properties設定和擷取介面
    2 c/c++語言 properties設定和擷取介面
    3 Java語言 properties設定和擷取的介面


           Android提供了一套系統參數設定和擷取的方法,這些參數可以在android系統中靜態或動態設定和擷取,使用SystemProperty類(該類只有在framework或系統級應用中使用),代碼中大量存在:SystemProperties.set()/SystemProperties.get();通過這兩個介面可以對系統的屬性進行讀取/設定,顧名思義系統屬性,肯定對整個系統全域共用。通常程式的執行以進程為單位各自相互獨立,如何?全域共用呢?

        System Properties(SystemProperty詳細介紹及原理)是怎麼一回事,又是如何?的呢?

       屬性系統是android的一個重要特性。它作為一個服務運行,管理系統配置和狀態。所有這些配置和狀態都是屬性。

每個屬性是一個鍵值對(key/value pair),其類型都是字串。

這些屬性可能是有些資源的使用狀態,進程的執行狀態,系統的特有屬性……
 命令列(shell) properties設定和擷取介面

getprop “key”

setprop "key" "value"

c/c++語言 properties設定和擷取介面

代碼定義在:

    
system/core/libcutils/properties.c #define PROPERTY_KEY_MAX   32#define PROPERTY_VALUE_MAX  92 int property_get(const char *key, char*value, const char *default_value);int property_set(const char *key, constchar *value);int property_list(void (*propfn)(const char *key, constchar *value, void*cookie), void *cookie);   

使用:

#include "cutils/properties.h" char prop[PROPERTY_VALUE_MAX] = "000";if(property_get("prop_name", prop, NULL) != 0) {    ...}

說明:

    property_get返回值為get到的屬性值的字串長度.
    prop_name為屬性名稱.
    prop為存放屬性值字串的地方.

 Java語言 properties設定和擷取的介面

frameworks/base/core/java/android/os/SystemProperties.java

    
<span style="font-size:14px;">public static final int PROP_NAME_MAX = 31;public static final int PROP_VALUE_MAX = 91;public static String get(String key) ;public static String get(String key, String def) ;public static int getInt(String key,int def) ;public static long getLong(String key,long def) ;public static boolean getBoolean(String key, boolean def) ;public static void set(String key, String val) ;public static void addChangeCallback(Runnable callback) ;</span>


遇到問題解決:

1.在Setting中添加開關使用getProperty()和setProperty()屬性來進行屬性的擷取和設定,在命令列使用


adb shell getprop


來查看屬性的值:

[persist.service.adb.enable]: [][persist.sys.ams.recover]: [false]<span style="color:#FF0000;">[persist.sys.boot_music_control]: [1]</span>[persist.sys.bootpackage]: [1][persist.sys.btn_auto_light]: [1]

2.在C++中擷取不到build.prop中設定的屬性值:

<span style="color:#FF0000;">persist.sys.boot_music_control</span>

解決:

                        //Modfy by jjli,control the boot and shutdown music char boot_music_control[PROPERTY_VALUE_MAX];property_get("persist.sys.boot_music_control", boot_music_control, 1);int boot_music_value = atoi(boot_music_control);if (boot_music_value == 1) {mediastatus = mediaplayer->setDataSource(pBackupSoundFileName, NULL);


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.