How to request UserDictionary content provider on Android?

I cannot find good code for an example of how to request a word from the UserDictionary content provider. My query looks like this:

Cursor cur = getContentResolver().query( UserDictionary.Words.CONTENT_URI, new String[] {Words._ID, Words.WORD}, Words.WORD + "=?", new String[] {"test"}, null); 

I also tried not to specify the query, nor to indicate the projection, and the cursor is always empty. I have included android.permission.READ_USER_DICTIONARY in my manifest.

+7
android
source share
3 answers

try it

 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); 

I have not tested this, just a suggestion

+1
source share

Prerequisite: Make sure you have the appropriate words in the UserDictionary on
SettingsLanguage & InputPersonal dictionary .

A sample sql query that searches for words containing SO in a user dictionary, and an equivalent Android code sample. Pay attention to use ? to replace with args .

SQL query:

 SELECT UserDictionary.Words._ID, UserDictionary.Words.WORD FROM UserDictionary.Words.CONTENT_URI WHERE UserDictionary.Words.WORD LIKE "%SO% 

Equivalent code:

 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); } } 

Permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/> sub>

0
source share

Starting with API 23, the user dictionary is only accessible via IME and spell checking. https://developer.android.com/reference/android/provider/UserDictionary.html

android.permission.READ_USER_DICTIONARY permission is no longer available in M

0
source share

All Articles