我似乎找不到任何关于如何在给定单词的情况下查询UserDictionary内容提供者的良好示例代码。我的查询如下:
Cursor cur = getContentResolver().query(
UserDictionary.Words.CONTENT_URI,
new String[] {Words._ID, Words.WORD},
Words.WORD + "=?",
new String[] {"test"},
null);
我也尝试过不指定查询以及不指定投影,并且光标始终为空。我在我的清单中加入了android.permission.READ_USER_DICTIONARY
。
答案 0 :(得分:1)
试试这个
final String[] QUERY_PROJECTION = {
UserDictionary.Words._ID,
UserDictionary.Words.WORD
};
Cursor cursor = getContentResolver()
.query(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, "(locale IS NULL) or (locale=?)",
new String[] { Locale.getDefault().toString() }, null);
我没有测试过这个,只是一个建议
答案 1 :(得分:1)
前提条件:确保在用户词典中有适当的单词
Settings
- > Language & Input
- > Personal dictionary
。
示例sql查询,用于搜索用户词典中包含SO
的单词及其等效的Android代码示例。请注意?
替换为args
的用法。
SQL查询:
SELECT UserDictionary.Words._ID, UserDictionary.Words.WORD FROM UserDictionary.Words.CONTENT_URI WHERE UserDictionary.Words.WORD LIKE "%SO%
等效代码:
String[] columns = {UserDictionary.Words._ID, UserDictionary.Words.WORD};
String condition = UserDictionary.Words.WORD + " LIKE ? ";
// ? in condition will be replaced by `args` in order.
String[] args = {"%SO%"};
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, columns, condition, args, null);
//Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null); - get all words from dictionary
if ( cursor != null ) {
int index = cursor.getColumnIndex(UserDictionary.Words.WORD);
//iterate over all words found
while (cursor.moveToNext()) {
//gets the value from the column.
String word = cursor.getString(index);
Log.i(TAG, "Word found: " + word);
}
}
AndroidManifest.xml中的权限:
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
子>
答案 2 :(得分:0)
M 中不再提供从API 23开始,用户词典只能通过IME访问 和拼写检查。 https://developer.android.com/reference/android/provider/UserDictionary.html
android.permission.READ_USER_DICTIONARY权限