繁体   English   中英

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

[英]How do I get a phone number from a ContentResolver with a single query?

我正在使用下面的代码来获取联系人姓名和ID,但没有得到电话号码。 如何从下面的代码中获取电话号码?

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();
}

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;

}

每个联系人的“ ContactsContract.Contacts”中没有电话号码,但“ ContactsContract.Contacts._ID”和“ ContactsContract.CommonDataKinds.Phone.CONTACT_ID”相同。 因此,您可以从'ContactsContract.CommonDataKinds.Phone.CONTENT_URI'中获取电话号码。 将此代码放入游标的循环中。 总体而言,这是两个查询,但至少您知道在哪里搜索。 这不需要花费很多时间来执行。

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));

        }

编辑:这是我在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();

供参考: 在android中阅读所有联系人的电话号码



基于我以前写给联系人的一些以前的工作,这篇较旧的帖子似乎很有帮助: 如何在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();
    } 

当然,您需要在查询中确保您从联系人中获取了正确的数据段(即更新投影),但是希望可以为您指明正确的方向!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM