static void setIntField(JNIEnv* env, jobject obj, const char* path,
jfieldID fieldID) {
const int SIZE = 128;
char buf[SIZE] = "\0";
jint value = 0;
if (readFromFile(path, buf, SIZE) > 0) {
value = atoi(buf);
}
env->SetIntField(obj, fieldID, value);
}
static int readFromFile(const char* path, char* buf, size_t size) {
if (!path)
return -1;
int fd = open(path, O_RDONLY, 0);
if (fd == -1) {
LOGE("Could not open '%s'", path);
return -1;
}
size_t count = read(fd, buf, size);
if (count > 0) {
count = (count < size) ? count : size - 1;
while (count > 0 && buf[count - 1] == '\n')
count--;
buf[count] = '\0';
} else {
buf[0] = '\0';
}
close(fd);
return count;
}
这是我的 Android JNI 代码。为什么我读取的路径相同,但是两次的值不一样。我正在阅读这条路径/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
。
如何解决?
scaling_min_freq
值是51000。第一次得到51000。第二次得到150000。