1
查詢內容我創建一個Android的Widget方含一些聯繫信息從Android小工具
我發現getContentResolver() - 方法只存在於活動類,而不是在的AppWidgetProvider類。
如何從小部件查詢內容?
像這樣:
光標人= getContentResolver()查詢(某些查詢);
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);
peopleStr += people.getString(nameFieldColumnIndex) + "\n";
}
people.close();
return peopleStr;
}
沒有。沒有工作。 – 2010-11-02 12:51:43
我嘗試將它插入主屏幕後,程序/小部件崩潰。我在原帖中添加了一個示例。 – 2010-11-02 12:52:13
沒關係。我忘記了在AndroidManifest.xml中添加READ_CONTACTS權限 – 2010-11-02 13:05:48