查询Android小部件中的内容

时间:2010-11-02 10:07:57

标签: android android-widget

我正在创建包含一些联系信息的Android Widget

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

如何查询小部件中的内容?

像这样:

Cursor people = 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;
    }

1 个答案:

答案 0 :(得分:2)

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