android L SystemProperties属性解析

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值