如何通过单个查询从ContentResolver获取电话号码?

时间:2016-06-25 10:13:32

标签: android android-contentresolver

我使用下面的代码获取联系人姓名和身份证,但我没有收到电话号码。如何从下面的代码中获取电话号码?

ContentResolver contentResolver = getBaseContext().getContentResolver();

Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.Contacts.HAS_PHONE_NUMBER,
        ContactsContract.Contacts.STARRED,
        ContactsContract.Contacts.TIMES_CONTACTED,
        ContactsContract.Contacts.LAST_TIME_CONTACTED
};
String selection = String.format("%s > 0", ContactsContract.Contacts.HAS_PHONE_NUMBER);
String[] selectionArgs = null;
String sortOrder = String.format(
        "%s DESC, %s DESC, %S DESC, UPPER(%s) ASC",
        ContactsContract.Contacts.STARRED,
        ContactsContract.Contacts.TIMES_CONTACTED,
        ContactsContract.Contacts.LAST_TIME_CONTACTED,
        ContactsContract.Contacts.DISPLAY_NAME
);
Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);

if (cursor.getCount() > 0) {
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


        if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

            System.out.println("name : " + name + ", ID : ");

         }

        }
    }
    cursor.close();
}

3 个答案:

答案 0 :(得分:1)

CModel.java

public class CModel {
String firstname;
String lastname;
String contactno;
String Imagepath;
int ID;
String contactid;

public String getCheck() {
    return Check;
}

String Check;

public String getContactid() {
    return contactid;
}

 public CModel(String fnm, String lastname, String userprofile, int ID,    String contactno, String Check) {
    this.firstname = fnm;
    this.lastname = lastname;
    this.Imagepath = userprofile;
    this.ID = ID;
    this.contactno = contactno;
    this.Check = Check;
}


public String getFirstname() {
    return firstname;
}

public String getLastname() {
    return lastname;
}

public String getContactno() {
    return contactno;
}

public String getImagepath() {
    return Imagepath;
}

public int getID() {
    return ID;
}
活动文件中的

调用此方法

private ArrayList<CModel> getcontact() {
ArrayList<CModel> contactlist = new ArrayList<CModel>;
try {
    Bitmap my_btmp = null;
    String profilepic;
    String phone = null;
    contactlist = new ArrayList<CModel>();
    ContentResolver cr = getContentResolver();
    String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null,
            ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    while (cur.moveToNext()) {
        String contactId = cur.getString(cur.getColumnIndex(ContactsContract.Data._ID));
        String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
        Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId));
        InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), my_contact_Uri);
        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[]{contactId}, null);
        while (pCur.moveToNext()) {
            phone = pCur.getString(
                    pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        }
        pCur.close();
        if (photo_stream != null) {
            BufferedInputStream buf = new BufferedInputStream(photo_stream);
            my_btmp = BitmapFactory.decodeStream(buf);
            profilepic = BitMapToString(my_btmp);
        } else {
            Bitmap bitmap = BitmapFactory.decodeResource(HomePage.this.getResources(), R.drawable.profilepic);
            my_btmp = bitmap;
            profilepic = BitMapToString(my_btmp);

        }
        String columns[] = {
                ContactsContract.CommonDataKinds.Event.START_DATE,
                ContactsContract.CommonDataKinds.Event.TYPE,
                ContactsContract.CommonDataKinds.Event.MIMETYPE,
        };
        String where =ContactsContract.CommonDataKinds.Event.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "' and " + ContactsContract.Data.CONTACT_ID + " = " + contactId;
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;
        Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder);
        if (birthdayCur.getCount() > 0) {
            if (birthdayCur.moveToFirst()) {
                do {

                    contactlist.add(new CModel(displayName, "", profilepic, 0, phone,"phone"));
                    boolean flag = con.comparedata(phone);

                } while (birthdayCur.moveToNext());

            }

        }
        birthdayCur.close();
    }
    cur.close();
} catch (Exception e) {
}
return contactlist;

}

答案 1 :(得分:1)

&#39; ContactsContract.Contacts&#39;中没有电话号码。但是&#39; ContactsContract.Contacts._ID&#39;和&#39; ContactsContract.CommonDataKinds.Phone.CONTACT_ID&#39;每个联系人都是一样的。因此,您可以从&#39; ContactsContract.CommonDataKinds.Phone.CONTENT_URI&#39;中获取电话号码。 。将此代码放在光标的循环中。总的来说,这是两个查询,但至少你知道在哪里搜索。这不应该花费太多时间执行。

Cursor phoneCursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId , null, null);

        while (phoneCursor.moveToNext()) {
           String phone = phoneCursor.getString(phoneCursor
                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

        }

答案 2 :(得分:0)

编辑:这是我尝试检索1个查询中的每个电话号码但我不确定是否存在没有电话号码的联系人;通常,最佳做法是始终查询具有电话号码的联系人列表,然后仅查询这些联系人号码。

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
    String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

    //Do something with each name and phone somewhere each time you loop through; in your provided code you seemed to be printing them out

}
phones.close();

供参考:Read all contact's phone numbers in android

这篇较旧的帖子似乎很有帮助,基于我之前为写信给联系人所做的一些工作:How to get contacts' phone number in Android

通常,电话号码存储为:

ContactsContract.CommonDataKinds.Phone.NUMBER 

所以,如果你拿一段你提供的代码,我会大致编辑:

if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


            ...
            ...

        cursor.close();
    } 

当然,您需要在查询中确保从联系人那里获取正确的数据段(即更新投影),但这应该会指向正确的方向!