Android源码--SystemProperties 和 Settings.system
来源:互联网 发布:python黑帽子中文网盘 编辑:程序博客网 时间:2024/06/08 04:27
1,SystemProperties
Android源码开发中大量存在SystemProperties.set()、SystemProperties.get(),这两个方法用于对系统属性的读取。这里以Android设置系统时区为例简单介绍一下SystemProperties的使用。
Settings/src/com/android/settings/ZonePicker.java
public void onListItemClick(ListView listView, View v, int position, long id) { ... final AlarmManager alarm = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE); alarm.setTimeZone(tzId); ... }
点击时区列表后调用AlarmManagerService的setTimeZone进行修改时区,设置系统属性值,并且发送修改时区广播.
frameworks/base/services/java/com/android/server/AlarmManagerService.java
public void setTimeZone(String tz) { mContext.enforceCallingOrSelfPermission("android.permission.SET_TIME_ZONE","setTimeZone");//判断权限
try { if (TextUtils.isEmpty(tz)) return; synchronized (this) { String current = <strong>SystemProperties.get("persist.sys.timezone")//获取系统之前的时区 if (current == null || !current.equals(zone.getID())) { timeZoneWasChanged = true; SystemProperties.set("persist.sys.timezone", zone.getID()); //设置最新的时区 } } if (timeZoneWasChanged) {
<span style="white-space:pre"></span>//发送修改时区广播 Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED); intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); intent.putExtra("time-zone", zone.getID()); mContext.sendBroadcastAsUser(intent, UserHandle.ALL); }
}设置默认时区可以在system.prop 修改persist.sys.timezone=Asia/Shanghai(如果没有需要自己添加)
需要注意的是android属性的名称是有一定的格式要求的,如下: 前缀必须用system\core\init\property_service.c中定义的前缀 ,进行系统属性设置的程序也必须有system或root权限。
如果属性名称以“ro.”开头,那么这个属性被视为只读属性。一旦设置,属性值不能改变。
如果属性名称以“persist.”开头,当设置这个属性时,其值也将写入/data/property
在adb shell可以通过以下的命名读取和修改
#getprop persist.sys.language
#setprop persist.sys.language zh
framework通过SystemProperties接口操作系统属性,SystemProperties通过JNI调用访问系统属性。
public class SystemProperties{ //JNI private static native String native_get(String key, String def); private static native void native_set(String key, String def); public static String get(String key, String def) { return native_get(key, def); } public static void set(String key, String val) { native_set(key, val); }}
Jni代码位置:
/frameworks/base/core/jni/android_os_SystemProperties.cpp
2,Settings.system
这种方式会保存变量到Settings 数据库中,通过get和put的方式对其中的数据进行读写
//其中"getXXX"代表对应的类似方法,如getInt()、getBoolean、putString()等。
//通过变量名称获取值,如果发生上面方法中导致异常的情况,将返给定的默认值
Settings.System.getXXX(ContentResolver cr, String name, XXX def);
//将指定名称的值写入数据库
Settings.System.putXXX(ContentResolver cr, String name, XXX Value);
也可以添加一个自定义的全局变量,可以参考设置系统亮度:
...
SCREEN_BRIGHTNESS,
- Android源码--SystemProperties 和 Settings.system
- Android SystemProperties和Settings.System
- Android SystemProperties和Settings.System介绍
- Android SystemProperties和Settings.System介绍
- Android SystemProperties和Settings.System介绍
- Android SystemProperties和Settings.System介
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 给android添加系统属性:Settings.system和SystemProperties
- 添加系统属性:Settings.system和SystemProperties
- SystemProperties与Settings.system
- SystemProperties与Settings.system
- SystemProperties与Settings.system
- 算法学习建议
- WPF学习 第十五章 动画基础
- 负数如何用二进制表示
- spring mvc DispatcherServet url-pattern / 与/*
- 史上最详细的CocoaPods安装教程
- Android源码--SystemProperties 和 Settings.system
- oracle trigger 字段触发
- 页面登陆框老是乱乱的?banner跨页图片缩小之后总是在侧面不能显示主要部分?哈哈~我来帮你忙~~
- oracle 新建表空间,参数修改,lv扩容
- Linux命令整理-awk
- ANSI,ASCII,Unicode的区别与联系
- 【原创】《Linux高级程序设计》杨宗德著 - Linux多线程编程 - 线程概念及创建线程
- C#读取和写入Word
- 机智采购竟让老业务员倒贴钱的生意