Android Settings DB - Settings.Global/SystemProperties 数据持久化存取

1.Settings DB usage:

https://developer.android.com/reference/android/provider/Settings.Global

import android.provider.Settings;

private void setTime(int flag) {
    Settings.Global.putInt(mContext.getContentResolver(), "SAP_AUTO_OFF_TIME", flag);
}

private int getTime() {
    return Settings.Global.getInt(mContext.getContentResolver(), "SAP_AUTO_OFF_TIME", 0);
}

//
Settings.Global.putLong
//Settings.Global.getLong


2.SystemProperties usage:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/SystemProperties.java
import android.os.SystemProperties;

SystemProperties.set("ro.vendor.sap.atg",true);
SystemProperties.set("ro.vendor.sap.atg", flag == 0 ? false : true);
isPassAutoGen = SystemProperties.get("ro.vendor.sap.atg", true);

 3.C/C++中property获取办法:

Android系统10 RK3399 init进程启动(三十六) 属性property操作API
https://blog.csdn.net/ldswfun/article/details/126374626

1.C语言客户端API

头文件在: system/core/include/cutils/properties.h
实现在: system/core/libcutils/properties.cpp

// #include <cutils/properties.h>

int8_t property_get_bool(const char *key, int8_t default_value);
int64_t property_get_int64(const char *key, int64_t default_value);
int property_get(const char* key, char* value, const char* default_value);
int32_t property_get_int32(const char *key, int32_t default_value);
int property_set(const char *key, const char *value);

 

2.C++客户端API
头文件声明在: system/core/base/include/android-base/properties.h
实现在: system/core/base/properties.cpp

// #include <android-base/properties.h>

namespace android {
    namespace base {
        // 获取bool类型的属性值,如果属性为空或者不存在, 返回参数2的默认值
        bool GetBoolProperty(const std::string& key, bool default_value);
        template <typename T> 
        //获取int类型的属性值, 如果属性为空或不存在,并且不在最小值和最大值之间,返回参数2的默认值
        T GetIntProperty(const std::string& key, T default_value, T min, T max);
        //获取unsignd int类型的属性值,如果属性为空或不存在,并且不在0和最大值之间,返回参数2的默认值
        template <typename T>
        T GetUintProperty(const std::string& key, T default_value, T max);
        // 获取默认类型( 字符串)属性值 
        std::string GetProperty(const std::string& key, const std::string& default_value);
        //设置属性值
        bool SetProperty(const std::string& key, const std::string& value);
        #if defined(__BIONIC__)
        // 等待属性的值变成预定值, relative_timeout参数设置超时时间, 成功返回true。
        bool WaitForProperty(const std::string& key, const std::string& expected_value,
                             std::chrono::milliseconds relative_timeout = std::chrono::milliseconds::max());
        #endif
    }
}

4.服务端C++修改属性的实现API
C和C ++客户端的设置都是通过socket和服务端通信, 服务端中会实现对应的接口

system/core/init/property_service.cpp

uint32_t PropertySet(const std::string& name, const std::string& value, std::string* error)

posted @ 2024-09-10 16:07  petercao  阅读(26)  评论(0编辑  收藏  举报