Android 属性property_get/property_set

1、property_get和property_set使用方法

property_get和property_set使用方法

一.使用方法
每个属性都有一个名称和值,他们都是字符串格式。属性被大量使用在Android系统中,用来记录系统设置或进程之间的信息交换。属性是在整个系统中全局可见的。每个进程可以get/set属性。
如下面我们可以在系统中代码设置属性或命令行设置属性,用于系统中的判断,

 setprop WLAN.SWITCH.FLAG 1
 getprop WLAN.SWITCH.FLAG

函数原型:

int property_get(const char *key, char *value, const char *default_value);
int property_set(const char *key, const char *value);

#define PROPERTY_VALUE_MAX 250
char propertyValue[PROPERTY_VALUE_MAX] = "\0";
if(property_get("WLAN.SWITCH.FLAG", propertyValue, NULL) > 0) {
    printf("WLAN.SWITCH.FLAG = %s\n", propertyValue);
    if(strncmp(propertyValue, "1", strlen("1")) == 0) {
        printf("wlan.switch.flag = on\n");
    } else {
        printf("wlan.switch.flag = off\n");
    }
} else {
    printf("property_get wlan.switch.flag failed\n");
}

二.相关知识
    1.加载的属性会被保持在/data/property中.
    2.编译时需要链接相应的动态库文件libcutils.so.
    3.需要添加头文件.

————————————————
2、property_get使用注意事项

返回值永远大于0.

之前虽然一直使用property_get函数,但是没有真正了解过,所以写出了这样一个bug

char buf[PROPERTY_VALUE_MAX] = {‘\0’};
if(property_get(“debug.property.test”, buf, “0”)) {
ALOGI(“true”);
} else {
ALOGI(“false”);
}

结果就是一直打印true

我们看下property_get源码,O版本路径system/core/libcutils/properties.cpp

114 int property_get(const char *key, char *value, const char *default_value) {
115 int len = __system_property_get(key, value);
116 if (len > 0) {
117 return len;
118 }
119 if (default_value) {
120 len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
121 memcpy(value, default_value, len);
122 value[len] = ‘\0’;
123 }
124 return len;
125}

错误的使用了default_value,导致返回值永远大于0;
如果想判断有没有这个property,所以正确调用是
if(property_get(“debug.property.test”, buf, NULL) > 0) {
}
————————————————

3、返回值

/* 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);

————————————————

4、使用

eg1:

static void sFastTrackMultiplierInit()
{
    char value[PROPERTY_VALUE_MAX];
    if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
        char *endptr;
        unsigned long ul = strtoul(value, &endptr, 0);
        if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
            sFastTrackMultiplier = (int) ul;
        }
    }
}

 eg2:

 void AudioFlinger::MmapPlaybackThread::checkSilentMode_l()
{
    if (!mMasterMute) {
        char value[PROPERTY_VALUE_MAX];
        if (property_get("ro.audio.silent", value, "0") > 0) {
            char *endptr;
            unsigned long ul = strtoul(value, &endptr, 0);
            if (*endptr == '\0' && ul != 0) {
                ALOGD("Silence is golden");
                // The setprop command will not allow a property to be changed after
                // the first time it is set, so we don't have to worry about un-muting.
                setMasterMute_l(true);
            }
        }
    }
}

你可能感兴趣的:(android语法,学习笔记,android)