您的位置:首页 > 其它

Andrid——获取手机中联系人详细信息

2015-04-02 16:24 49 查看
嘛,转载的文章木有说从哪里转的哟....

贴上转载地址吧~
http://blog.csdn.net/flying_vip_521/article/details/7064488
private void getAllInfo(){
// 获得所有的联系人
Cursor cur = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI,null,null,null,
ContactsContract.Contacts.DISPLAY_NAME      + " COLLATE LOCALIZED ASC");
// 循环遍历
if (cur.moveToFirst()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

do {
// 获得联系人的ID号
String contactId = cur.getString(idColumn);
// 获得联系人姓名
String disPlayName = cur.getString(displayNameColumn);

// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cur .getInt(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
System.out.println("!!!  username:  "+disPlayName);
if (phoneCount > 0) {
// 获得联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID   + " = " + contactId, null, null);
if (phones.moveToFirst()) {
do {
// 遍历所有的电话号码
String phoneNumber = phones .getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phoneType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
System.out.println("!!!!  phoneNumber:"+phoneNumber);
System.out.println("!!!!  phoneType:"+ phoneType);
} while (phones.moveToNext());
}
}

// 获取该联系人邮箱
Cursor emails = getContentResolver().query(  ContactsContract.CommonDataKinds.Email.CONTENT_URI,  null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID  + " = " + contactId, null, null);
if (emails.moveToFirst()) {
do {
// 遍历所有的电话号码
String emailType = emails .getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String emailValue = emails .getString(emails .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
System.out.println("!!!!  emailType: "+emailType);
System.out.println("!!!!!  emailValue: "+emailValue);
} while (emails.moveToNext());
}

// 获取该联系人IM
Cursor IMs = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Im.PROTOCOL, Im.DATA },
Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
+ Im.CONTENT_ITEM_TYPE + "'",
new String[] { contactId }, null);
if (IMs.moveToFirst()) {
do {
String protocol = IMs.getString(IMs
.getColumnIndex(Im.PROTOCOL));
String date = IMs
.getString(IMs.getColumnIndex(Im.DATA));
System.out.println("!!!! protocol: "+protocol);
System.out.println("!!!!! date:  "+date);
} while (IMs.moveToNext());
}

// 获取该联系人地址
Cursor address = getContentResolver()
.query(
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (address.moveToFirst()) {
do {
// 遍历所有的地址
String street = address
.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = address
.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String region = address
.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postCode = address
.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String formatAddress = address
.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
System.out.println("!!!!  street: "+ street);
System.out.println("!!!! city: "+city);
System.out.println("!!!!  region:  "+region);
System.out.println("!!!!  postCode:  "+postCode);
System.out.println("!!!!  formatAddress:  "+formatAddress);
} while (address.moveToNext());
}

// 获取该联系人组织
Cursor organizations = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Organization.COMPANY,
Organization.TITLE },
Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
+ Organization.CONTENT_ITEM_TYPE + "'",
new String[] { contactId }, null);
if (organizations.moveToFirst()) {
do {
String company = organizations.getString(organizations
.getColumnIndex(Organization.COMPANY));
String title = organizations.getString(organizations
.getColumnIndex(Organization.TITLE));
System.out.println("!!!!  company: "+company);
System.out.println("!!!!  title: "+title);
} while (organizations.moveToNext());
}

// 获取备注信息
Cursor notes = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Note.NOTE },
Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"    + Note.CONTENT_ITEM_TYPE + "'",
new String[] { contactId }, null);
if (notes.moveToFirst()) {
do {
String noteinfo = notes.getString(notes  .getColumnIndex(Note.NOTE));
System.out.println("!!!!!  noteinfo:  "+noteinfo);
} while (notes.moveToNext());
}

// 获取nickname信息
Cursor nicknames = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Nickname.NAME },
Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"   + Nickname.CONTENT_ITEM_TYPE + "'",
new String[] { contactId }, null);
if (nicknames.moveToFirst()) {
do {
String nickname_ = nicknames.getString(nicknames
.getColumnIndex(Nickname.NAME));
System.out.println("!!!!!  nickname_:"+nickname_);
} while (nicknames.moveToNext());
}

} while (cur.moveToNext());

}

}


特殊的是,联系人的生日和周年纪念日都不能通过这样的方式获取,应该如下

ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String [] projection = new String[]{Event.DATA1};
String selection = Data.MIMETYPE+"='"+Event.CONTENT_ITEM_TYPE+"'"+" and "+Event.TYPE+"='"+Event.TYPE_BIRTHDAY+"'";
Cursor brithdayCursor = cr.query(uri, projection, selection, null, null);
if(brithdayCursor!=null){
if(brithdayCursor.moveToFirst()){
do{
System.out.println("!!!!! curso birthday : "+brithdayCursor.getString(0));
}while(brithdayCursor.moveToNext());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: