SystemPropertiesでのAndroidの使用



Use Android Systemproperties



SystemPropertiesは、いくつかの単純なデータを保存するために使用されるAndroidシステムで非常に広く使用されており、非常に簡単に使用できます

SystemProperties.set('persist.test.flycom', '1') SystemProperties.get('persist.test.flycom', '2');

ブール値、整数値、長整数値も取得できます



/** * Get the value for the given key, and return as an integer. * @param key the key to lookup * @param def a default value to return * @return the key parsed as an integer, or def if the key isn't found or * cannot be parsed */ public static int getInt(String key, int def) { if (TRACK_KEY_ACCESS) onKeyAccess(key) return native_get_int(key, def) } /** * Get the value for the given key, and return as a long. * @param key the key to lookup * @param def a default value to return * @return the key parsed as a long, or def if the key isn't found or * cannot be parsed */ public static long getLong(String key, long def) { if (TRACK_KEY_ACCESS) onKeyAccess(key) return native_get_long(key, def) } /** * Get the value for the given key, returned as a boolean. * Values 'n', 'no', '0', 'false' or 'off' are considered false. * Values 'y', 'yes', '1', 'true' or 'on' are considered true. * (case sensitive). * If the key does not exist, or has any other value, then the default * result is returned. * @param key the key to lookup * @param def a default value to return * @return the key parsed as a boolean, or def if the key isn't found or is * not able to be parsed as a boolean. */ public static boolean getBoolean(String key, boolean def) { if (TRACK_KEY_ACCESS) onKeyAccess(key) return native_get_boolean(key, def) }

ここでは、問題を説明するための特別な許可があります。説明する許可がない場合、次のエラーが発生します。

07-05 03:24:48.541 6328-6350/? E/AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: android.display java.lang.RuntimeException: failed to set system property at android.os.SystemProperties.native_set(Native Method) at android.os.SystemProperties.set(SystemProperties.java:155) at com.android.server.am.ActivityManagerService.finishBooting(ActivityManagerService.java:7504) at com.android.server.am.ActivityManagerService.bootAnimationComplete(ActivityManagerService.java:7527) at com.android.server.wm.WindowManagerService.performEnableScreen(WindowManagerService.java:3621) at com.android.server.wm.WindowManagerService.-wrap6(Unknown Source:0) at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:5349) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.os.HandlerThread.run(HandlerThread.java:65) at com.android.server.ServiceThread.run(ServiceThread.java:46)

内部でアプリを使用している場合は、デバイス mediatek sepolicy bsp non_plat property_contextsを内部に追加するだけです



persist.test.flycom u:object_r:system_prop:s0

署名とAndroidアプリには、sharedUserId = 'android.uid.system'というシステムが必要です。システム内で開発するのではなく、リフレクションを使用して呼び出すことができます。

public public String getProperty(String key, String defaultValue) { String value = defaultValue try { Class c = Class.forName('android.os.SystemProperties') Method get = c.getMethod('get', String.class, String.class) value = (String)(get.invoke(c, key, 'unknown' )) } catch (Exception e) { e.printStackTrace() }finally { return value } } public void setProperty(String key, String value) { try { Class c = Class.forName('android.os.SystemProperties') Method set = c.getMethod('set', String.class, String.class) set.invoke(c, key, value ) } catch (Exception e) { e.printStackTrace() } }

フレームワークレイヤーを使用している場合は、3つのファイルを変更する必要があります。

device / mediatek / sepolicy / bsp / non_plat / property.te



type flycom_test_prop, property_type, mtk_core_property_type

device / mediatek / sepolicy / bsp / non_plat / property_contexts

persist.test.flycom u:object_r:flycom_test_prop:s0

device / mediatek / sepolicy / bsp / non_plat / system_server.te

allow system_server flycom_test_prop:property_service set

もちろん、sys、devにandroid独自のプロパティを使用してpersist.sysを開始できます。デフォルトは特権であり、追加する必要はありません。