用户字典ContentResolver查询返回0行光标

时间:2015-11-30 23:21:31

标签: android android-contentresolver

我正在尝试使用Android开发人员指南中的类似ContentProvider读取 - 从用户词典中读取。我无法理解为什么我的光标总是0行..当然我已经添加了所需的权限。我试图将空值更改为空数组或类似的东西,但它无法正常工作。 有什么想法吗?

有我的源代码

    public void sampleMethodWithContentProviders() {

//        String contentUriStr = "content://user_dictionary/words";
//        Uri wordsUri = Uri.parse(contentUriStr);

        String[] projectionString = {
                UserDictionary.Words._ID,
                UserDictionary.Words.WORD,
                UserDictionary.Words.LOCALE
        };

        String selectClause = null;

        String[] selectArgs = null;

        String order = null;

        Cursor cursor = getContentResolver().query(UserDictionary.Words.CONTENT_URI, projectionString, selectClause, selectArgs, order);


        Log.i("Cursor", cursor == null ? "null" : "obiekt");
        for (String s : cursor.getColumnNames())
            Log.i("Cursor column name: ", s);
        Log.i("Num of cursor element: ", Integer.toString(cursor.getCount()));
        while (cursor.moveToNext())
            Log.i("a", "b");
    }

还有日志结果

  

12-01 00:02:54.530 11693-11693 / com.kros.withoutpain I / Cursor:obiekt   12-01 00:02:54.530 11693-11693 / com.kros.withoutpain   I / Cursor列名:: _id 12-01 00:02:54.530   11693-11693 / com.kros.withoutpain I / Cursor column name :: word 12-01   00:02:54.530 11693-11693 / com.kros.withoutpain I / Cursor列名::   locale 12-01 00:02:54.530 11693-11693 / com.kros.withoutpain   I / Num of cursor element :: 0

1 个答案:

答案 0 :(得分:2)

从API 23向上从Android中删除了对用户词典的访问权限,请参阅this bug report...this stack overflow question...

似乎这已经关闭,因为“按预期工作”似乎有一些滥用该功能,因此它被删除。

如果您正在尝试使用模拟器,则可能需要尝试使用早期版本的APK。然而,我没有太多运气回到21(Lollipop)。如果您只是尝试使用游标和内容解析器,那么我建议Muhammad Farag's answer to a similar question