1. 程式人生 > >Settings.System的使用

Settings.System的使用

那通過資料庫來儲存就會有Provider了,所以就會有SettingsProvider了。資料庫的路徑就是:/data/data/com.android.providers.settings/databases/settings.db

但我們平常獲取這裡面的資料不是直接通過ContentResolve而是android已經封裝了一層,通過Settings這個類來獲取,就像MediaStore一樣。 
比如我們獲取飛航模式:

Java程式碼  
  1. Settings.System.getInt(mContext.getContentResolver(),  
  2.                 Settings.System.AIRPLANE_MODE_ON, 0
    )  

通過name,查詢資料庫,獲取你想要的值,如果沒有取到,就預設為def.否則就是其返回的值

public static int getInt (ContentResolver cr, String name, int def)

Since: API Level 3

Convenience function for retrieving a single secure settings value as an integer. Note that internally setting values are always stored as strings; this function converts the string to an integer for you. The default value will be returned if the setting is not defined or not an integer.

Parameters
cr The ContentResolver to access.
name The name of the setting to retrieve.
def Value to return if the setting is not defined.
Returns
  • The setting's current value, or 'def' if it is not defined or not a valid integer.
//更改name對應的value值

public static boolean putInt (ContentResolver cr, 
String
 name, int value)

Since: API Level 3

Convenience function for updating a single settings value as an integer. This will either create a new entry in the table if the given name does not exist, or modify the value of the existing row with that name. Note that internally setting values are always stored as strings, so this function converts the given value to a string before storing it.

Parameters
cr The ContentResolver to access.
name The name of the setting to modify.
value The new value for the setting.
Returns
  • true if the value was set, false on database errors
更多contentprovider的資料,看:http://www.eoeandroid.com/thread-29089-1-1.html