`
whotodo
  • 浏览: 168440 次
文章分类
社区版块
存档分类
最新评论

SystemProperties与Settings.system

 
阅读更多

android源码开发中,常常要用到一些全局标志或者说变量,这时候我们可以给android系统添加自己想要的属性。

1.Settings.system

这种系统属性我们经常用到,例如飞行模式的开启和关闭,我们就是去改变Settings.system.AIRPLANE_MODE_ON 的值。

下面就介绍下我们怎样去定义一个系统属性。例如要添加一个属性名为“SILVAN_LIU”

路径:frameworks/base/core/java/android/provider/Settings.java

  1. publicstaticfinalStringSILVAN_LIU="silvan_liu";
  2. publicstaticfinalString[]SETTINGS_TO_BACKUP={
  3. ~
  4. +SILVAN_LIU
  5. ~
  6. }
这样添加后,你就是可以通过Settings.System.getInt(getContentResolver(), Settings.System.SILVAN_LIU,0) 和Settings.System.getInt(getContentResolver(), Settings.System.SILVAN_LIU,0)去得到和设定SILVAN_LIU的属性值。

2.SystemProperties

创建与修改android属性用Systemproperties.set(name, value),获取android属性用Systemproperties.get(name),需要注意的是android属性的名称是有一定的格式要求的,如下:前缀必须用system\core\init\property_service.c中定义的前缀,进行系统属性设置的程序也必须有system或root权限

如果我们要添加一个property:例如:silvan_liu

路径:system/core/rootdir/int.rc

在on post-fs-data 目录下

setprop persist.sys.silvan_liu 1//persist.sys 前缀名; 1为初始值

PS:不同前缀名权限不同,这里就不一一说明;还有为什么要加载on post-fs-data目录下,这和int.rc的语法有关。

1 使用 SystemProperties.get

这中方法在framework里面 经常见到。

特别属性

如果属性名称以“ro.”开头,那么这个属性被视为只读属性。一旦设置,属性值不能改变。
如果属性名称以“persist.”开头,当设置这个属性时,其值也将写入/data/property。

在c++中就是对应JAVA的两个函数就是property_set, property_get,其实JAVA是通过JNI调用这两个函数的。


JAVA

import android.os.SystemProperties;

SystemProperties.set("persist.sys.language", zone.getID());
String lang= SystemProperties.get("persist.sys.language");

C

#include <cutils/properties.h>

property_set("persist.sys.language", "zh");
property_get("persist.sys.language", propLang, "en");

在adb shell可以通过以下的命名读取和修改

#getprop persist.sys.language


#setprop persist.sys.language zh

2 使用 Settings.System.putInt

这种方式会保存变量到Settings 数据库中,飞行模式等的开关就是用这种方式实现的。


AndroidManifest.xml中包含权限:
<uses-permission android:name=”android.permission.WRITE_SETTINGS” />
在需要发送数据的java文件中,包含:
import android.provider.Settings;
import android.content.ContentResolver;
ContentResolver resolver = mContext.getContentResolver();
Settings.System.putInt(resolver,”inputFlag”,1);
同理,在接收数据端:
Settings.System.getInt(resolver,”inputFlag”,3);
mContext为所在应用的上下文。

分享到:
评论

相关推荐

    The Definitive Guide to NetBeans Platform

    ■CHAPTER 3 The Module System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ■CHAPTER 4 Actions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    Published.rar

    &lt;add name="huProj.PMS.Properties.Settings.JJErpDBConnectionString" connectionString="Data Source=172.19.2.142;Initial Catalog=JJErpDB;Persist Security Info=True;User ID=sa;Password=XXXXXXXXX" ...

    Mitec_System.Info_14.5.0.zip

    MiTeC System Information Component Suite The most complex system information probe in Delphi and FPC/Lazarus world. The cost of a site license with full source codes is currently 110 EUR. TMiTeC_...

    FDTD用户手册

    Initial Properties dialog box ................................................................................... 33 Mask Export...........................................................................

    LTM Config 配置手册打包

    Configuring virtual address properties and settings ................................................... 2-14 Managing virtual servers and virtual addresses ...............................................

    仓底货-进销存源码改进版2012813

    &lt;add name="CangDiHuo.Models.Properties.Settings.CangDiHuoConnectionString" connectionString="改为你自己的字符串" providerName="System.Data.SqlClient" /&gt; 这个数据库连接字符串中的...

    myeclipse for spring 10.1 crack

    目的是把生成的LICENSE_KEY, ACTIVATION_CODE, ACTIVATION_KEY保存到C:\Documents and Settings\Administrator\.myeclipse.properties文件中 4. 点击菜单'Tools -&gt; 1.ReplaceJarFile', 选择${eclipse_home}/plugins...

    myEclipse2013破解工具

    目的是把生成的LICENSE_KEY, ACTIVATION_CODE, ACTIVATION_KEY保存到C:\Documents and Settings\Administrator\.myeclipse.properties文件中 4. 点击菜单'Tools -&gt; 1.ReplaceJarFile', 选择${eclipse_home}/plugins...

    cadstar 11 用户手册 (英文)

    1 System Overview........................................................................................1 1.1 Block Diagram...............................................................................

    The Automotive Chassis: Engineering Principles [2 ed.]

    4.1 Steering system......Page 278 4.1.3 Steering system on rigid axles......Page 281 4.2.1 Advantages and disadvantages......Page 283 4.2.2 Configurations......Page 284 4.2.3 Steering gear, manual ...

    Administering VMware Site Recovery Manager 4.0.pdf

    Creating a New File System ................................................................................. 33 Creating an iSCSI LUN ....................................................................

    MyEclipse 10.5破解补丁(免费)

    目的是把生成的LICENSE_KEY, ACTIVATION_CODE, ACTIVATION_KEY保存到C:\Documents and Settings\Administrator\.myeclipse.properties文件中 4. 点击菜单'Tools -&gt; 1.ReplaceJarFile', 选择${eclipse_home}/plugins...

    spring-boot-reference.pdf

    9. System Requirements 9.1. Servlet Containers 10. Installing Spring Boot 10.1. Installation Instructions for the Java Developer 10.1.1. Maven Installation 10.1.2. Gradle Installation 10.2. Installing...

    MyEclipse破解工具

    目的是把生成的LICENSE_KEY, ACTIVATION_CODE, ACTIVATION_KEY保存到C:\Documents and Settings\Administrator\.myeclipse.properties文件中 4. 点击菜单'Tools -&gt; 1.ReplaceJarFile', 选择${eclipse_home}/...

    仓底货-进销存源码2012517

    &lt;add name="CangDiHuo.Models.Properties.Settings.CangDiHuoConnectionString" connectionString="Data Source=.\\sql2008;Initial Catalog=CangDiHuo;uid=sa;pwd=sa" providerName="System.Data.SqlClient" /&gt; ...

    web dynpro abap programming guidelines

    Contents WEB DYNPRO ABAP: DEVELOPMENT IN DETAIL .............................................................. 1 1 BASICS..................................................................................

    EMS Advanced Data Import VCL suite v3.11.0.rar

    properties were set to True. Fixed now. 10. Null values were not replaced correctly with custom values. Fixed now. 11. Now empty strings ('') are stored correctly in the configuration file.

    Advanced Data Import 3.11 10.3 Rio

    properties were set to True. Fixed now. 10. Null values were not replaced correctly with custom values. Fixed now. 11. Now empty strings ('') are stored correctly in the configuration file.

    hibernate.properties

    ### Miscellaneous Settings ### ############################## ## print all generated SQL to the console #hibernate.show_sql true ## format SQL in log and console hibernate.format_sql true ## ...

    myeclipse 10.5 破解

    目的是把生成的LICENSE_KEY, ACTIVATION_CODE, ACTIVATION_KEY保存到C:\Documents and Settings\Administrator\.myeclipse.properties文件中 4. 点击菜单'Tools -&gt; 1.ReplaceJarFile', 选择${eclipse_home}/plugins...

Global site tag (gtag.js) - Google Analytics