android M 设置 db 查询错误与 LIKE 关键字

android M settings db query error with LIKE keyword

下面的代码在 L 版本上工作正常,但在 M 版本上不起作用。

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

但在 M 版本中,出现以下错误:

 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)

我应该如何更改我的代码以同时适用于 L 和 M?

是的,M 设置似乎没有使用数据库,而是使用以下 xml 文件来存储信息:

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

这是否意味着任何数据库查询都会失败?

如@lucky1928 所说,它们现在存储在纯 XML 文件中。 对这些设置的访问应该是相同的,除了现在它们都应该更安全。

发件人:SettingsProvider.java

现在有关于如何存储这些设置的解释 (Android 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.