android 屬性系統使用

這裡說明一下具體使用場景及方法:

需求:有一個應用需要操作mtd flash,這是一層比較底級的接口操作,針對此類接口封裝好瞭libmtd_flash.so,但是同時
碰到一個問題,應用對設備沒有訪問權限,多放查證增加瞭對sdcard的訪問等還是不行,那麼解決這個問題的方法就是兩個:
1、在c++層做一個service,使用binder作為客戶端聯系橋梁
2、使用android自帶的屬性系統
從使用的場景及數據量,還是復雜度多方面考慮,決定采用屬性系統共享數據

1、首先我們知道:
   Native代碼
    當編寫Native的程序時,可以使用property_get和property_set API來獲得和設置屬性。使用這兩個API必須要包含頭文件和鏈接libcutil庫。
   Java代碼
    Android在Java庫中提供System.getProperty和System.setProperty方法,我們Java程序可以通過他們來設置和獲得屬性。
   由於我們在native層編寫代碼,所以使用property_get和property_set即可
  
2、列一下屬性系統的接口函數

/* property_get: returns the length of the value which will never be
** greater than PROPERTY_VALUE_MAX – 1 and will always be zero terminated.
** (the length does not include the terminating zero).
**
** If the property read fails or returns an empty value, the default
** value is used (if nonnull).
*/
int property_get(const char *key, char *value, const char *default_value);

/* property_set: returns 0 on success, < 0 on failure
*/
int property_set(const char *key, const char *value);

ok,沒有什麼初始化或者啥的函數,直接使用set、get即可,相當方便

3、實現使用
a、直接編譯一個可執行後臺進程運行
b、在些中讀取和設定mtd flash中數據,比如open/read/write/erase等等操作,將其數據保存到內存中,對於
   隻需要get的屬性數據直接從內存中查找即可
c、對於write/erase等對flash中數據改寫的操作,同步內存中數據及屬性系統中數據即可
d、對於上層應用或其它進程需要訪問相關數據直接利用property_get/property_set即可

 

優點:一個後臺進程,服務於所有客戶端並保持數據一致,最重要的解決瞭權限問題,因為由init啟動的服務進程帶有root權

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *