如何从联系电话中读取联系人姓名?

时间:2012-08-08 11:03:14

标签: android

我正在尝试在列表视图中显示联系人号码和联系人姓名。 我做了......:

public String DisplayName(String number) {
    Uri uri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(number));
    String name = "?";

    ContentResolver contentResolver = getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {
            BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
            null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup
                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            // String contactId =
            // contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}

但我的应用程序继续关闭,我不知道为什么?我也包含了许可...... “”

任何猜测?为什么

3 个答案:

答案 0 :(得分:4)

使用这两种方法:

/*
 * Returns contact's id
 */
private String getContactId(String phoneNumber, Context context) {
    ContentResolver mResolver = context.getContentResolver();

    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));

    Cursor cursor = mResolver.query(uri, new String[] {
            PhoneLookup.DISPLAY_NAME, PhoneLookup._ID }, null, null, null);

    String contactId = "";

    if (cursor.moveToFirst()) {
        do {
            contactId = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup._ID));
        } while (cursor.moveToNext());
    }

    cursor.close();
    cursor = null;
    return contactId;
}

/*
 * Returns contact's name
 */
private String getContactName(String contactId, Context context) {

    String[] projection = new String[] { Contacts.DISPLAY_NAME };
    Cursor cursor = mResolver.query(Contacts.CONTENT_URI, projection,
            Contacts._ID + "=?", new String[] { contactId }, null);
    String name = "";
    if (cursor.moveToFirst()) {
        name = cursor.getString(0);
    }

    cursor.close();
    cursor = null;
    return name;
}

答案 1 :(得分:0)

使用以下代码:

    public void readContacts(){
    ContentResolver cr = parent.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
           null, null, null, null);

    if (cur.getCount() > 0) {
       while (cur.moveToNext()) {
           Contact tempcontact=new Contact();
           contactsList.add(tempcontact);
            String id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           tempcontact.setId(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)));
           tempcontact.setName(name);
           if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
              Log.e("DATA","name : " + name + ", ID : " + id);

               // get the phone number
               Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                      new String[]{id}, null);
               while (pCur.moveToNext()) {
                   if(pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))!=null)
                   {
                   tempcontact.getPhonenumber().add(pCur.getString(
                           pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                   }
                  //   System.out.println("phone" + phone);
                    // Log.e("phone",phone);
               }
               pCur.close();


               // get email and type

              Cursor emailCur = cr.query(
                       ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                       null,
                       ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                       new String[]{id}, null);
               while (emailCur.moveToNext()) {
                   // This would allow you get several email addresses
                       // if the email addresses were stored in an array
                   String email = emailCur.getString(
                                 emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                   tempcontact.getEmail().add(emailCur.getString(
                                 emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
                   String emailType = emailCur.getString(
                                 emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                   Log.e("Data","Email " + email + " Email Type : " + emailType);
               }
               emailCur.close();


               String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
               String[] addrWhereParams = new String[]{id,
                   ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
               Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
                           null, null, null, null);
               while(addrCur.moveToNext()) {
                   tempcontact.setPoBox((addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX))));
                   tempcontact.setPoBox(addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
                   tempcontact.setCity(addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
                   tempcontact.setRegion(addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
                   tempcontact.setPostalCode(addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
                   tempcontact.setCountry((addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY))));
                   tempcontact.setType(addrCur.getString(
                                addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)));

                   // Do something with these....

               }
               addrCur.close();



               String[] projection = {ContactsContract.CommonDataKinds.Photo.PHOTO};
               Uri uri = (Uri)ContactsContract.Data.CONTENT_URI;
               String where = ContactsContract.Data.MIMETYPE 
                      + "=" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + " AND " 
                      + ContactsContract.Data.CONTACT_ID + " = " + id;
               Cursor cursor = parent.getContentResolver().query(uri, projection, null, null, null);

               if(cursor!=null&&cursor.moveToFirst()){
                   do{
                       byte[] photoData = cursor.getBlob(0);
                      tempcontact.setPhoto(photoData);

                       //Do whatever with your photo here...
                   }while(cursor.moveToNext());
               }



           }
       }

  }
  }


public class Contact {
String id;
String name;
ArrayList<String> phonenumber;
ArrayList<String> email;
String poBox;
String street;
String city;
String state;
String postalCode;
String country;
String type;
String region;
byte[] photo;
public byte[] getPhoto() {
    return photo;
}

public Contact() {
// TODO Auto-generated constructor stub
phonenumber=new ArrayList<String>();
email=new ArrayList<String>();
}

public void setPhoto(byte[] photo) {
    this.photo = photo;
}

public String getPoBox() {
    return poBox;
}

public void setPoBox(String poBox) {
    this.poBox = poBox;
}

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public ArrayList<String> getPhonenumber() {
    return phonenumber;
}

public void setPhonenumber(ArrayList<String> phonenumber) {
    this.phonenumber = phonenumber;
}

public ArrayList<String> getEmail() {
    return email;
}

public void setEmail(ArrayList<String> email) {
    this.email = email;
}

public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}

}

答案 2 :(得分:0)

Cursor phones= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{

  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
phones.close();