Querying content from Android widget
I am creating an Android Widget containg some contact info
I discovered that the getContentResolver()-method only exists in the Activity-class, and not in the AppWidgetProvider-class.
How do I query content from a widget?
Like so:
Cursor people = getContentResolver().query(SOME QUERY);
UPDATE:
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { String people = getContacts(context); // Do something with people... } private String getContacts(Context context) { String peopleStr = ""; String[] projection = new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME }; String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'"; Cursor people = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, selection, null, sortOrder); while(people.moveToNext()) { int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.Data.DISPLAY_NAME); 开发者_JAVA技巧 peopleStr += people.getString(nameFieldColumnIndex) + "\n"; } people.close(); return peopleStr; }
getContentResolver() is actually defined in Context class. That means, it is possible to call that thru the context passed in appwidgetprovider.onupdate() and those related methods.
精彩评论