android L SystemProperties属性解析

来源:互联网 发布:mysql 5.7.17.tar.gz 编辑:程序博客网 时间:2024/06/16 01:21

android L版本关机流程中,最终通过修改系统属性sys.powerctl shutdown执行关机操作,通过JNI调用访问系统属性。Android调用系统属性流程如下:

native_set()<SystemProperties.java>--->SystemProperties_set()<android_os_SystemProperties.cpp>

framework/base/core/java/android/os/SystemProperties.java

这是SystemProperties.java类中设置系统函数的方法。

   /**
     * Get the value for the given key.
     * @return an empty string if the key isn't found
     * @throws IllegalArgumentException if the key exceeds 32 characters
    */
    public static String get(String key) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get(key);//SystemProperties.java通过JNI调用访问系统属性
    }

    /**
     * Get the value for the given key.
     * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static String get(String key, String def) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get(key, def);SystemProperties.java通过JNI调用访问系统属性
    }

SystemProperties接口类在初始环境中注册对应CPP接口android_os_SystemProperties.cpp,实际操作通过JNI调用对应cpp文件,frameworks/base/core/jni/AndroidRuntime.cpp.点击查看源码

frameworks/base/core/jni/android_os_SystemProperties.cpp;点击查看完整源码

int register_android_os_SystemProperties(JNIEnv *env)
{
    return AndroidRuntime::registerNativeMethods(
        env, "android/os/SystemProperties",
        method_table, NELEM(method_table));
}
};


 static void SystemProperties_set(JNIEnv *env, jobject clazz,   jstring keyJ, jstring valJ)
 {
     int err;
    const char* key;
    const char* val;
 if (keyJ == NULL) {
         jniThrowNullPointerException(env, "key must not be null.");
        return ;
    }
    key = env->GetStringUTFChars(keyJ, NULL);
/*从java程序中传过去的String对象在本地方法中对应的是jstring类型,jstring类型和c中的char*不同,如果你直接作为char*使用的话,就会出错。因此使用之前需要进行转换。转换方式就是GetStringUTFChars(keyJ, NULL)<JNIenv方式>,即将jstring转换成UTF-8格式的char.*/

    if (valJ == NULL) {
        val = "";       /* NULL pointer not allowed here */
    } else {
       val = env->GetStringUTFChars(valJ, NULL);
     } 
    err = property_set(key, val); 
    env->ReleaseStringUTFChars(keyJ, key);
    /*释放指向UTF-8格式的char*的指针*/
     if (valJ != NULL) {
       env->ReleaseStringUTFChars(valJ, val);
     }
    if (err < 0) {
       jniThrowException(env, "java/lang/RuntimeException",  "failed to set system property");
  }
}


0 0
原创粉丝点击