【问题标题】:Get Android local contacts获取 Android 本地联系人
【发布时间】:2015-07-16 19:16:23
【问题描述】:

是否可以获取官方联系人应用中显示的联系人?

我尝试用这种方式检索联系人:

    contentResolver = ApplicationSingleton.getInstance().getContentResolver();
    String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.STARRED};
    String selection = null;
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, projection, selection, null, null);


    while (cursor.moveToNext()) {
        String name = (cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        Log.d("CONTACT: ", name);
    }

    cursor.close();

但随后我获得了与我的电子邮件相关联的所有联系人。

谢谢。

【问题讨论】:

标签: android android-contacts


【解决方案1】:

使用这个,

  private void displayContacts() {

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                }
                pCur.close();
            }
        }
    }
}
猜你喜欢
  • 2013-12-16
  • 2023-03-06
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多