setting.system-全局属性的设定

SystemProperties跟Settings.System

 

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

JAVA代码如下:

import android.os.SystemProperties;

//设定

SystemProperties.set("persist.sys.language", "默认值");

//读取
String lang= SystemProperties.get("persist.sys.language");//string

int lang = SystemProperties.getInt("persist.sys.language",10);

boolean fastfoodenable = SystemProperties.getBoolean("persist.sys.fastfoodenable", false);//boolean

 

创建与修改android属性用

Systemproperties.set(name, value);

 

获取android属性用

Systemproperties.get(name);

 

需要注意的是android属性的名称是有一定的格式要求的,

 前缀必须用system\core\init\property_service.c中定义的前缀

property_service.c的内容如下

property_perms[] = { 
    { "net.rmnet0.",      AID_RADIO,    0 },
    { "net.gprs.",        AID_RADIO,    0 },
    { "net.ppp",          AID_RADIO,    0 },
    { "net.qmi",          AID_RADIO,    0 },
    { "net.lte",          AID_RADIO,    0 },
    { "net.cdma",         AID_RADIO,    0 },
    { "ril.",             AID_RADIO,    0 },
    { "persist.ril.",     AID_RADIO,    0 },
    { "persist.ril.cfu.querytype", AID_APP,   0 },
    { "mux.",             AID_RADIO,    0 },
    { "mux.",             AID_SYSTEM,   0 },
    { "mtk_telephony_mode_slot1",             AID_SYSTEM,   0 },
    { "mtk_telephony_mode_slot2",             AID_SYSTEM,   0 },
    { "gsm.",             AID_RADIO,    0 },
    { "persist.radio",    AID_RADIO,    0 },
    { "net.dns",          AID_RADIO,    0 },
    { "sys.usb.config",   AID_RADIO,    AID_SYSTEM },
    { "net.",             AID_SYSTEM,   0 },
    { "net.",             AID_DHCP,   0 },
    { "dev.",             AID_SYSTEM,   0 },
    { "runtime.",         AID_SYSTEM,   0 },
    { "hw.",              AID_SYSTEM,   0 },
    { "sys.",             AID_SYSTEM,   0 },
    { "sys.powerctl",     AID_SHELL,    0 },
    { "service.",         AID_SYSTEM,   0 },
    { "wlan.",            AID_SYSTEM,   0 },
    { "bluetooth.",       AID_BLUETOOTH,   0 },
    { "dhcp.",            AID_SYSTEM,   0 },
    { "bwc.mm.",          AID_SYSTEM,   0 },
    { "dhcp.",            AID_DHCP,     0 },
    { "debug.",           AID_SYSTEM,   0 },
    { "debug.",           AID_SHELL,    0 },
    { "log.",             AID_SHELL,    0 },
    { "service.adb.root", AID_SHELL,    0 },
    { "service.adb.tcp.port", AID_SHELL,    0 },
    { "persist.sys.",     AID_SYSTEM,   0 },
    { "persist.service.", AID_SYSTEM,   0 },
    { "persist.security.", AID_SYSTEM,   0 },
    { "persist.service.bdroid.", AID_BLUETOOTH,   0 },
    { "selinux."         , AID_SYSTEM,   0 },
    { "gps.",             AID_GPS,     AID_SYSTEM },
    { "persist.af.",      AID_MEDIA,   0 },

 

进行系统属性设置的程序也必须有 system或root权限

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

路径:

system/core/rootdir/init.rc

它的内容如下

on post-fs-data
    # We chown/chmod /data again so because mount is run as root + defaults
    chown system system /data
    chmod 0771 /data
    # We restorecon /data in case the userdata partition has been reset.
    restorecon /data

    # Avoid predictable entropy pool. Carry over entropy from previous boot.
    copy /data/system/entropy.dat /dev/urandom

    # Create dump dir and collect dumps.
    # Do this before we mount cache so eventually we can use cache for
    # storing dumps on platforms which do not have a dedicated dump partition.
    mkdir /data/dontpanic 0750 root log

    # Collect apanic data, free resources and re-arm trigger
    copy /proc/apanic_console /data/dontpanic/apanic_console
    chown root log /data/dontpanic/apanic_console
    chmod 0640 /data/dontpanic/apanic_console

    copy /proc/apanic_threads /data/dontpanic/apanic_threads
    chown root log /data/dontpanic/apanic_threads
    chmod 0640 /data/dontpanic/apanic_threads

    write /proc/apanic_console 1
    # If there is no fs-post-data action in the init.<device>.rc file, you
    # must uncomment this line, otherwise encrypted filesystems
    # won't work.
    # Set indication (checked by vold) that we have finished this action
    #setprop vold.post_fs_data_done 1

 

在on post-fs-data 目录下

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

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

 

2 使用 Settings.System.putInt

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

首先需要定义一个系统属性值

路径

frameworks/base/core/java/android/provider/Settings.java

添加一个属性标签

  public static final String QS_DYNAMIC_WIFI = "qs_dyanmic_wifi";

        /**  
         * Quick Settings Quick Pulldown
         *
         * @hide
         */
        public static final String QS_QUICK_PULLDOWN = "qs_quick_pulldown";

        /**  
         * Quick Settings Collapse Pane
         *
         * @hide
         */
        public static final String QS_COLLAPSE_PANEL = "qs_collapse_panel";

        /**  
         * Quick Settings Quick access ribbon
         *
         * @hide
         */
        public static final String QS_QUICK_ACCESS = "qs_quick_access";


//别忘了在这里面提添加
 public static final String[] SETTINGS_TO_BACKUP = {
            QS_QUICK_PULLDOWN
            STAY_ON_WHILE_PLUGGED_IN,   // moved to global
            WIFI_USE_STATIC_IP,
            WIFI_STATIC_IP,
            WIFI_STATIC_GATEWAY,
            WIFI_STATIC_NETMASK,
            WIFI_STATIC_DNS1,
            WIFI_STATIC_DNS2,
            BLUETOOTH_DISCOVERABILITY,
            BLUETOOTH_DISCOVERABILITY_TIMEOUT,
            DIM_SCREEN,
            SCREEN_OFF_TIMEOUT,
            SCREEN_BRIGHTNESS,
            SCREEN_BRIGHTNESS_MODE,
            SCREEN_AUTO_BRIGHTNESS_ADJ,
            VIBRATE_INPUT_DEVICES,
            MODE_RINGER_STREAMS_AFFECTED,
            VOLUME_VOICE,
            VOLUME_SYSTEM,
            VOLUME_RING,
            VOLUME_MUSIC,
            VOLUME_ALARM,
}

 

1)获取方法如下:

 

Settings.System.getInt(getContentResolver(), Settings.System.QS_QUICK_PULLDOWN,0);

第三个参数是默认值

2)设置

Settings.System.putInt(getContentResolver(),Settings.System.QS_QUICK_PULLDOWN, 1);

代码中使用的时候,需要导入包

需要import android.provider.Settings;

posted @ 2015-04-14 13:18  小白屋  阅读(3792)  评论(0编辑  收藏  举报