android L SystemProperties属性解析
2016-01-25 17:56
537 查看
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");
}
}
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");
}
}
相关文章推荐
- Android Studio 打包apk,自动追加版本号和版本名称
- Android FragmentActivity 和Activity的关系
- Android Binder机制浅析
- RxAndroid+Retrofit环境搭建
- Android 循环广告位轮播图的封装实现
- RecyclerView的使用
- Android 系统发送短信给多个联系人使用
- Android数据存储实现的5大方式
- Android开发——多媒体开发之MediaPlayer
- android PopupWindow详解
- 最近遇到的一些Bug的处理
- Android开发之EditText属性详解
- android5.0适配——轻松实现acitivty跳转View无缝过渡动画
- android从fragment跳转到fragment问题解决
- Android程序混淆代码
- Android设计模式源码解析之外观模式(Facade)
- 浅谈Android开发中内存泄露与优化
- 在Android Studio 和 Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
- Android 4.3 menu item showAsAction=“always”无效的解决办法
- Android Style和Theme