2016-01-20 39 views
0

下面的代碼在L上工作正常,但在M版本上不起作用。Android M設置db LIKE關鍵字查詢錯誤

public void test() { 
    ContentResolver resolver = getContentResolver(); 
    String[] proj = { 
      Settings.System.NAME, 
      Settings.System.VALUE}; 
    String sql = proj[1] + " LIKE 'content%'"; 
    Cursor cursor = resolver.query(Settings.System.CONTENT_URI, 
      proj,sql,null,null); 
    while(cursor.moveToNext()) { 
     String key = cursor.getString(cursor.getColumnIndex(proj[0])); 
     String value = cursor.getString(cursor.getColumnIndex(proj[1])); 
     Log.e(TAG,"" + key + "->" + value); 
    } 
    cursor.close(); 
} 

輸入L版本,輸出是:

11595 01-20 11:55:36.322 15833 15833 E MyExam : alarm_alert->content://media/internal/audio/media/11 
    11596 01-20 11:55:36.322 15833 15833 E MyExam : notification_sound->content://media/internal/audio/media/24 
    11597 01-20 11:55:36.322 15833 15833 E MyExam : ringtone->content://media/internal/audio/media/36 

但在併購的釋放,我得到了以下錯誤:

01-20 11:59:29.962 10141 10141 E AndroidRuntime: FATAL EXCEPTION: main 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: Process: com.colibri.gaplessplayer, PID: 10141 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: java.lang.IllegalArgumentException: Supported SQL: 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: uri content://some_table/some_property with null where and where args 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: uri content://some_table with query name=? and single name as arg 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: uri content://some_table with query name=some_name and null args 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: but got - uri:content://settings/system, where:value LIKE 'content%' whereArgs:null 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165) 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: at android.content.ContentProviderProxy.query(ContentProviderNative.java:421) 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:491) 
    01-20 11:59:29.962 10141 10141 E AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:434) 

我應該如何改變我的代碼,兩個大號工作和M?

+0

也許你沒有。 「ContentProvider」不需要支持任意SQL,因爲「ContentProvider」不需要將其數據存儲在SQL數據庫中。據推測,無論是設置已將其數據移動到非SQL數據存儲區,還是正在進行工作以爲將來的交換機做好準備。 – CommonsWare

回答

0

是,似乎中號設置不使用下面的XML文件數據庫但存儲信息:

/data/system/users/0/settings_global.xml 
/data/system/users/0/settings_secure.xml 
/data/system/users/0/settings_system.xml 

這是否意味着任何數據庫查詢將它失敗?

1

正如@ lucky1928所說,它們現在存儲在普通的XML文件中。 訪問這些設置應該是相同的,除了現在它應該更安全。

來源:SettingsProvider.java

有這些設置現在是如何的解釋(安卓M)存儲。

This class is a content provider that publishes the system settings. It can be accessed via the content provider APIs or via custom call commands. The latter is a bit faster and is the preferred way to access the platform settings.

There are three settings types, global (with signature level protection and shared across users), secure (with signature permission level protection and per user), and system (with dangerous permission level protection and per user). Global settings are stored under the device owner. Each of these settings is represented by a {@link com.android.providers.settings.SettingsState} object mapped to an integer key derived from the setting type in the most significant bits and user id in the least significant bits. Settings are synchronously loaded on instantiation of a SettingsState and asynchronously persisted on mutation. Settings are stored in the user specific system directory.

Apps targeting APIs Lollipop MR1 and lower can add custom settings entries and get a warning. Targeting higher API version prohibits this as the system settings are not a place for apps to save their state. When a package is removed the settings it added are deleted. Apps cannot delete system settings added by the platform. System settings values are validated to ensure the clients do not put bad values. Global and secure settings are changed only by trusted parties, therefore no validation is performed. Also there is a limit on the amount of app specific settings that can be added to prevent unlimited growth of the system process memory footprint.