這裡說明一下具體使用情境及方法:
需求:有一個應用需要操作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權