我正在创建一个包含一些联系信息的Android小部件

我发现getContentResolver()方法仅存在于Activity类中,而不存在于AppWidgetProvider类中。

如何从窗口小部件查询内容?

像这样:

游标人= getContentResolver()。query(SOME QUERY);

更新:

@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);
            peopleStr += people.getString(nameFieldColumnIndex) + "\n";
        }
        people.close();
        return peopleStr;
    }

最佳答案

getContentResolver()实际上是在Context类中定义的。这意味着,可以通过appwidgetprovider.onupdate()和相关方法中传递的上下文进行调用。

10-05 20:00