查询/设置Settings的值

本文介绍如何在Android系统中查询和设置Settings中的值,包括系统亮度、休眠时间等,并提供了具体的代码示例。文章还详细列出了可通过SettingsProvider访问的各种系统设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     按计划每周更新一篇技术博文,第四篇:《查询/设置Settings的值

    通常很多定制系统或者与系统相关的应用都需要查询/设置 Settings(设置)里边的值,比如系统亮度,休眠时间,锁屏之类的系统设置等,主要通过 SettingsProvider来查询/设置,前提是要知道所要修改的值的数据类型与字符串代表的name。
    对应的name值与数据类型在./ics/frameworks/base/core/java/android/provider/Settings.java文件中,例举两个例子如下:

1.查询休眠时间:
/**
* 获取系统休眠时间
* @return 秒数(从不待机 返回0)
*/
public static String getTimeOut(ContentResolver resolver){
int timeOut = 0;
try {
timeOut = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
timeOut /= 1000;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
String timeOutStr = timeOut + "";
return timeOutStr;
}

2.更改系统亮度:
/**
* 保存亮度设置状态
* 改变setting值
* @param resolver
* @param brightness : 0~1
*/
public static void saveBrightness(ContentResolver resolver, float brightness) {
Uri uri = android.provider.Settings.System
.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS,
(int)(brightness*255));
resolver.notifyChange(uri, null);
}

需要权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />



PS:

a.查询Settings中各个属性所对应的值(Android APIs):http://developer.android.com/reference/android/provider/Settings.System.html

b.ContentResolver Context提供getContentResolver()方法可以获取ContentResolver对象,便可以操作文中前边所提的 SettingsProvider所暴露的数据。
c.Android系统提供大量的SettingsProvider,Settings除外,常用的还有管理联系人(ContactsContract)、短信(Telephony)、多媒体数据(MediaStore)等,对应的URI可到这里查看:http://developer.android.com/reference/android/provider/package-summary.html,源码路径:./ics/frameworks/base/core/java/android/provider/
d.Settings默认初始值的位置(修改之前最好弄明白先,修改后再测试一下): frameworks/base/packages/SettingsProvider/res/values/defaults.xml


=========================================================================

Content Provider中的Setting.System表格内存储了很多系统属性的值,以键值对的形式存在!

可以对如下的变量进行设置

Constants
StringACCELEROMETER_ROTATIONControl whether the accelerometer will be used to change screen orientation.
StringADB_ENABLEDThis constant is deprecated. Use ADB_ENABLED instead
StringAIRPLANE_MODE_ONWhether Airplane Mode is on.
StringAIRPLANE_MODE_RADIOSA comma separated list of radios that need to be disabled when airplane mode is on.
StringALARM_ALERTPersistent store for the system-wide default alarm alert.
StringALWAYS_FINISH_ACTIVITIESIf 1, the activity manager will aggressively finish activities and processes as soon as they are no longer needed.
StringANDROID_IDThis constant is deprecated. Use ANDROID_ID instead
StringAPPEND_FOR_LAST_AUDIBLEAppended to various volume related settings to record the previous values before they the settings were affected by a silent/vibrate ringer mode change.
StringAUTO_TIMEValue to specify if the user prefers the date, time and time zone to be automatically fetched from the network (NITZ).
StringAUTO_TIME_ZONEValue to specify if the user prefers the time zone to be automatically fetched from the network (NITZ).
StringBLUETOOTH_DISCOVERABILITYDetermines whether remote devices may discover and/or connect to this device.
StringBLUETOOTH_DISCOVERABILITY_TIMEOUTBluetooth discoverability timeout.
StringBLUETOOTH_ONThis constant is deprecated. Use BLUETOOTH_ON instead
StringDATA_ROAMINGThis constant is deprecated. Use DATA_ROAMING instead
StringDATE_FORMATDate format string mm/dd/yyyy dd/mm/yyyy yyyy/mm/dd
StringDEBUG_APPName of an application package to be debugged.
StringDEVICE_PROVISIONEDThis constant is deprecated. Use DEVICE_PROVISIONEDinstead
StringDIM_SCREENWhether or not to dim the screen.
StringDTMF_TONE_WHEN_DIALINGWhether the audible DTMF tones are played by the dialer when dialing.
StringEND_BUTTON_BEHAVIORWhat happens when the user presses the end call button if they're not on a call.
StringFONT_SCALEScaling factor for fonts, float.
StringHAPTIC_FEEDBACK_ENABLEDWhether the haptic feedback (long presses, ...) are enabled.
StringHTTP_PROXYThis constant is deprecated. Use HTTP_PROXY instead
StringINSTALL_NON_MARKET_APPSThis constant is deprecated. UseINSTALL_NON_MARKET_APPS instead
StringLOCATION_PROVIDERS_ALLOWEDThis constant is deprecated. UseLOCATION_PROVIDERS_ALLOWED instead
StringLOCK_PATTERN_ENABLEDThis constant is deprecated. Use LOCK_PATTERN_ENABLEDinstead
StringLOCK_PATTERN_TACTILE_FEEDBACK_ENABLEDThis constant is deprecated. UseLOCK_PATTERN_TACTILE_FEEDBACK_ENABLED instead
StringLOCK_PATTERN_VISIBLEThis constant is deprecated. Use LOCK_PATTERN_VISIBLEinstead
StringLOGGING_IDThis constant is deprecated. Use LOGGING_ID instead
StringMODE_RINGERRinger mode.
StringMODE_RINGER_STREAMS_AFFECTEDDetermines which streams are affected by ringer mode changes.
StringMUTE_STREAMS_AFFECTEDDetermines which streams are affected by mute.
StringNETWORK_PREFERENCEThis constant is deprecated. Use NETWORK_PREFERENCEinstead
StringNEXT_ALARM_FORMATTEDA formatted string of the next alarm that is set, or the empty string if there is no alarm set.
StringNOTIFICATION_SOUNDPersistent store for the system-wide default notification sound.
StringPARENTAL_CONTROL_ENABLEDThis constant is deprecated. UsePARENTAL_CONTROL_ENABLED instead
StringPARENTAL_CONTROL_LAST_UPDATEThis constant is deprecated. UsePARENTAL_CONTROL_LAST_UPDATE instead
StringPARENTAL_CONTROL_REDIRECT_URLThis constant is deprecated. UsePARENTAL_CONTROL_REDIRECT_URL instead
StringRADIO_BLUETOOTHConstant for use in AIRPLANE_MODE_RADIOS to specify Bluetooth radio.
StringRADIO_CELLConstant for use in AIRPLANE_MODE_RADIOS to specify Cellular radio.
StringRADIO_NFCConstant for use in AIRPLANE_MODE_RADIOS to specify NFC radio.
StringRADIO_WIFIConstant for use in AIRPLANE_MODE_RADIOS to specify Wi-Fi radio.
StringRINGTONEPersistent store for the system-wide default ringtone URI.
StringSCREEN_BRIGHTNESSThe screen backlight brightness between 0 and 255.
StringSCREEN_BRIGHTNESS_MODEControl whether to enable automatic brightness mode.
intSCREEN_BRIGHTNESS_MODE_AUTOMATICSCREEN_BRIGHTNESS_MODE value for automatic mode.
intSCREEN_BRIGHTNESS_MODE_MANUALSCREEN_BRIGHTNESS_MODE value for manual mode.
StringSCREEN_OFF_TIMEOUTThe timeout before the screen turns off.
StringSETTINGS_CLASSNAMEThis constant is deprecated. Use SETTINGS_CLASSNAMEinstead
StringSETUP_WIZARD_HAS_RUNWhether the setup wizard has been run before (on first boot), or if it still needs to be run.
StringSHOW_GTALK_SERVICE_STATUS 
StringSHOW_PROCESSESControl whether the process CPU usage meter should be shown.
StringSHOW_WEB_SUGGESTIONSThis constant is deprecated. Each application that shows web suggestions should have its own setting for this.
StringSOUND_EFFECTS_ENABLEDWhether the sounds effects (key clicks, lid open ...) are enabled.
StringSTAY_ON_WHILE_PLUGGED_INWhether we keep the device on while the device is plugged in.
StringSYS_PROP_SETTING_VERSION 
StringTEXT_AUTO_CAPSSetting to enable Auto Caps in text editors.
StringTEXT_AUTO_PUNCTUATESetting to enable Auto Punctuate in text editors.
StringTEXT_AUTO_REPLACESetting to enable Auto Replace (AutoText) in text editors.
StringTEXT_SHOW_PASSWORDSetting to showing password characters in text editors.
StringTIME_12_24Display times as 12 or 24 hours 12 24
StringTRANSITION_ANIMATION_SCALEScaling factor for activity transition animations.
StringUSB_MASS_STORAGE_ENABLEDThis constant is deprecated. UseUSB_MASS_STORAGE_ENABLED instead
StringUSER_ROTATIONDefault screen rotation when no other policy applies.
StringUSE_GOOGLE_MAILThis constant is deprecated. Use USE_GOOGLE_MAILinstead
StringVIBRATE_ONWhether vibrate is on for different events.
StringVOLUME_ALARMAlarm volume.
StringVOLUME_BLUETOOTH_SCOBluetooth Headset volume.
StringVOLUME_MUSICMusic/media/gaming volume.
StringVOLUME_NOTIFICATIONNotification volume.
StringVOLUME_RINGRinger volume.
StringVOLUME_SYSTEMSystem/notifications volume.
StringVOLUME_VOICEVoice call volume.
StringWAIT_FOR_DEBUGGERIf 1, when launching DEBUG_APP it will wait for the debugger before starting user code.
StringWALLPAPER_ACTIVITYName of activity to use for wallpaper on the home screen.
StringWIFI_MAX_DHCP_RETRY_COUNTThis constant is deprecated. UseWIFI_MAX_DHCP_RETRY_COUNT instead
StringWIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MSThis constant is deprecated. UseWIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MSinstead
StringWIFI_NETWORKS_AVAILABLE_NOTIFICATION_ONThis constant is deprecated. UseWIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON instead
StringWIFI_NETWORKS_AVAILABLE_REPEAT_DELAYThis constant is deprecated. UseWIFI_NETWORKS_AVAILABLE_REPEAT_DELAY instead
StringWIFI_NUM_OPEN_NETWORKS_KEPTThis constant is deprecated. UseWIFI_NUM_OPEN_NETWORKS_KEPT instead
StringWIFI_ONThis constant is deprecated. Use WIFI_ON instead
StringWIFI_SLEEP_POLICYThe policy for deciding when Wi-Fi should go to sleep (which will in turn switch to using the mobile data as an Internet connection).
intWIFI_SLEEP_POLICY_DEFAULTValue for WIFI_SLEEP_POLICY to use the default Wi-Fi sleep policy, which is to sleep shortly after the turning off according to the STAY_ON_WHILE_PLUGGED_IN setting.
intWIFI_SLEEP_POLICY_NEVERValue for WIFI_SLEEP_POLICY to never go to sleep.
intWIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGEDValue for WIFI_SLEEP_POLICY to use the default policy when the device is on battery, and never go to sleep when the device is plugged in.
StringWIFI_STATIC_DNS1If using static IP, the primary DNS's IP address.
StringWIFI_STATIC_DNS2If using static IP, the secondary DNS's IP address.
StringWIFI_STATIC_GATEWAYIf using static IP, the gateway's IP address.
StringWIFI_STATIC_IPThe static IP address.
StringWIFI_STATIC_NETMASKIf using static IP, the net mask.
StringWIFI_USE_STATIC_IPWhether to use static IP and other static network attributes.
StringWIFI_WATCHDOG_ACCEPTABLE_PACKET_LOSS_PERCENTAGEThis constant is deprecated. UseWIFI_WATCHDOG_ACCEPTABLE_PACKET_LOSS_PERCENTAGEinstead
StringWIFI_WATCHDOG_AP_COUNTThis constant is deprecated. UseWIFI_WATCHDOG_AP_COUNT instead
StringWIFI_WATCHDOG_BACKGROUND_CHECK_DELAY_MSThis constant is deprecated. UseWIFI_WATCHDOG_BACKGROUND_CHECK_DELAY_MS instead
StringWIFI_WATCHDOG_BACKGROUND_CHECK_ENABLEDThis constant is deprecated. UseWIFI_WATCHDOG_BACKGROUND_CHECK_ENABLED instead
StringWIFI_WATCHDOG_BACKGROUND_CHECK_TIMEOUT_MSThis constant is deprecated. UseWIFI_WATCHDOG_BACKGROUND_CHECK_TIMEOUT_MSinstead
StringWIFI_WATCHDOG_INITIAL_IGNORED_PING_COUNTThis constant is deprecated. UseWIFI_WATCHDOG_INITIAL_IGNORED_PING_COUNT instead
StringWIFI_WATCHDOG_MAX_AP_CHECKSThis constant is deprecated. UseWIFI_WATCHDOG_MAX_AP_CHECKS instead
StringWIFI_WATCHDOG_ONThis constant is deprecated. Use WIFI_WATCHDOG_ONinstead
StringWIFI_WATCHDOG_PING_COUNTThis constant is deprecated. UseWIFI_WATCHDOG_PING_COUNT instead
StringWIFI_WATCHDOG_PING_DELAY_MSThis constant is deprecated. UseWIFI_WATCHDOG_PING_DELAY_MS instead
StringWIFI_WATCHDOG_PING_TIMEOUT_MSThis constant is deprecated. UseWIFI_WATCHDOG_PING_TIMEOUT_MS instead
StringWINDOW_ANIMATION_SCALEScaling factor for normal window animations.

通过如下的函数可以对其进行读写:

static void getConfiguration( ContentResolver cr,  Configuration outConfig)
Convenience function to read all of the current configuration-related settings into a Configuration object.
static float getFloat( ContentResolver cr,  String name, float def)
Convenience function for retrieving a single system settings value as a floating point number.
static float getFloat( ContentResolver cr,  String name)
Convenience function for retrieving a single system settings value as a float.
static int getInt( ContentResolver cr,  String name)
Convenience function for retrieving a single system settings value as an integer.
static int getInt( ContentResolver cr,  String name, int def)
Convenience function for retrieving a single system settings value as an integer.
static long getLong( ContentResolver cr,  String name, long def)
Convenience function for retrieving a single system settings value as a  long.
static long getLong( ContentResolver cr,  String name)
Convenience function for retrieving a single system settings value as a  long.
static boolean getShowGTalkServiceStatus( ContentResolver cr)
synchronized static  String getString( ContentResolver resolver,  String name)
Look up a name in the database.
static  Uri getUriFor( String name)
Construct the content URI for a particular name/value pair, useful for monitoring changes with a ContentObserver.
static boolean putConfiguration( ContentResolver cr,  Configuration config)
Convenience function to write a batch of configuration-related settings from a Configuration object.
static boolean putFloat( ContentResolver cr,  String name, float value)
Convenience function for updating a single settings value as a floating point number.
static boolean putInt( ContentResolver cr,  String name, int value)
Convenience function for updating a single settings value as an integer.
static boolean putLong( ContentResolver cr,  String name, long value)
Convenience function for updating a single settings value as a long integer.
static boolean putString( ContentResolver resolver,  String name,  String value)
Store a name/value pair into the database.
static void setShowGTalkServiceStatus( ContentResolver cr, boolean flag)


注意,应用程序中应加入: <uses-permission android:name="android.permission.WRITE_SETTINGS" />


<think>好的,我现在需要处理用户的问题:“location setting changed [u0]: content://settings/secure/location_mode这是什么意思”。首先,我需要理解用户的问题到底是什么。看起来用户遇到了一个日志条目,内容是“location setting changed [u0]: content://settings/secure/location_mode”,他们想知道这是什么意思。 首先,我应该分解这个日志的各个部分。日志中的“location setting changed”表示位置设置发生了变化。“[u0]”可能与用户账户有关,比如多用户环境下的用户标识。“content://settings/secure/location_mode”看起来像是一个ContentProvider的URI,指向具体的设置项。 接下来,我需要回忆Android系统中关于设置和ContentProvider的知识。Android中的Settings.Secure类用于管理安全相关的设置,比如位置模式。当某个设置项发生变化时,系统可能会通过ContentResolver通知监听者,这时候就会用到这样的URI。 然后,“u0”可能指的是用户ID,其中u0代表主用户(User 0)。在多用户设备上,不同的用户会有不同的标识符,比如u0、u1等。因此,这个日志可能表示主用户的位置设置发生了变化。 接下来,我需要确认URI的结构。content://settings/secure/location_mode是Settings.Secure中的LOCATION_MODE设置项的URI。当这个被修改时,系统会发出这个URI的通知,让监听该设置的应用知道发生了变化。 用户可能想知道这个日志的来源,或者他们是否应该关注这个信息。可能的情况包括应用在监听位置设置的变化,或者系统服务检测到设置变更后的记录。需要解释日志的各个部分,并说明可能的场景,比如用户手动更改了位置设置,或者某个应用修改了该设置。 另外,用户可能遇到问题,比如频繁出现这样的日志导致性能问题,或者想知道如何监听这类变化。这时候需要提供相关的代码示例,比如如何使用ContentResolver监听设置变化,或者如何通过ContentObserver来响应这样的变更。 还需要考虑权限问题,修改Secure设置通常需要系统权限,普通应用可能无法直接修改,但可以监听变化。如果用户是在开发应用,可能需要提醒他们处理权限和监听的正确方法。 总结回答的结构:首先解释日志的各个部分,然后说明可能的触发原因,接着提供如何监听或处理这种变化的示例,最后可能列出相关问题供用户进一步参考。 需要确保回答准确,特别是关于URI的结构和多用户标识的部分。可能还需要检查AOSP中的相关代码,确认URI的正确性以及u0的具体含义。此外,提供示例代码时,要确保代码正确有效,比如使用ContentResolver注册ContentObserver的方法。 最后,确保回答符合用户的需求,他们可能想知道这个日志的意义以及如何处理,或者是否正常。需要明确回答这些点,并提供有用的扩展信息,帮助用户深入理解。</think>这是 Android 系统中关于 **位置设置变化** 的日志,表示某个用户的 **位置模式(Location Mode)** 发生了更改。以下是对日志各部分的详细解析: --- ### 1. **日志结构拆解** ``` location setting changed [u0]: content://settings/secure/location_mode ``` | 部分 | 含义 | |------|------| | `location setting changed` | 位置设置发生变更 | | `[u0]` | 用户标识符(User 0,主用户) | | `content://settings/secure/location_mode` | 触发变更的设置项 URI | --- ### 2. **关键术语解释** #### (1) **`[u0]`:用户标识符(User Handle)** - **含义**:`u0` 表示当前操作属于 **主用户(Primary User)**,Android 多用户系统中用户标识符的格式为 `u<UserID>`。 - **多用户场景**: - `u0`:主用户(系统默认用户) - `u10`:第一个访客用户 - `u999`:系统用户(System User) - **作用**:区分不同用户的设置(如 `Secure` 设置支持用户隔离)。 #### (2) **`content://settings/secure/location_mode`:设置项 URI** - **含义**: - `content://settings/secure/`:`Settings.Secure` 的 ContentProvider URI 前缀。 - `location_mode`:具体设置项的键名(对应 `Settings.Secure.LOCATION_MODE`)。 - **底层机制**: 当设置项变更时,系统通过 `ContentResolver` 发送广播,URI 用于标识变更的具体设置项。 --- ### 3. **触发场景** 此日志通常在以下情况下出现: 1. **用户手动修改位置设置** - 例如在系统设置中开启/关闭位置服务。 2. **应用调用 `Settings.Secure.putInt()` 修改设置** - 需要系统权限(如 `WRITE_SECURE_SETTINGS`)。 3. **系统服务检测到设置变化** - 例如 `LocationManagerService` 监听到模式变更。 --- ### 4. **如何监听此类变更?** 若需监听位置模式变化,可通过 `ContentObserver` 实现: ```java // 示例:监听位置模式变化 mContext.getContentResolver().registerContentObserver( Settings.Secure.getUriFor(Settings.Secure.LOCATION_MODE), false, new ContentObserver(new Handler(Looper.getMainLooper())) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); // 处理位置设置变化 int newMode = Settings.Secure.getInt( mContext.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF ); Log.d("LocationMode", "新位置模式: " + newMode); } } ); ``` --- ### 5. **相关设置项与** #### (1) **`LOCATION_MODE` 的可能** | | 常量 | 含义 | |----|------|------| | `0` | `LOCATION_MODE_OFF` | 位置服务关闭 | | `1` | `LOCATION_MODE_ON` | 位置服务开启(基于网络和GPS) | | `3` | `LOCATION_MODE_HIGH_ACCURACY` | 高精度模式(强制使用GPS) | #### (2) **查看当前设置** 可通过 ADB 命令查询: ```bash adb shell settings get secure location_mode ``` --- ### 6. **常见问题与调试** 1. **为何频繁出现此日志?** - 可能是某个应用或服务频繁修改位置设置,需检查代码逻辑。 2. **如何区分不同用户的设置?** - 使用 `Settings.Secure.getIntForUser()` 并指定用户 ID。 3. **普通应用能否修改 `Secure` 设置?** - 否,需系统权限(如 `WRITE_SECURE_SETTINGS`),仅限系统应用或 root 设备。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值