做了几次通讯录了,但是每次都会忘,下面把自己用的关于通讯录的相关代码记录下来,很多都是来自于网上,分享的人总是那么可爱。分享充实人生。。。
贴出的方法都是我用在项目里面的,亲测可用。
先要加权限哟:
<!-- 读联系人权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!-- 写联系人权限 -->
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
/**
* 获取所有联系人,注:会有重复的联系人,建议自己处理一下
*
* @param context
* @return
*/
public static List<FriendItem> getBriefContactInfor(Context context) {
//定义常量,节省重复引用的时间
Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
String ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
//临时变量
String contactId;
String displayName;
//生成ContentResolver对象
ContentResolver contentResolver = context.getContentResolver();
// 获取手机联系人
Cursor cursor = contentResolver.query(Uri.parse("content://com.android.contacts/contacts"), null, null, null, null);
List<FriendItem> friendList = new ArrayList<FriendItem>();
List<String> names = new ArrayList<String>();//用来检测是否是重复的名字
// 无联系人直接返回
if (!cursor.moveToFirst()) {//moveToFirst定位到第一行
return null;
}
do {
// 获得联系人的ID:String类型 列名--》列数--》列内容
contactId = cursor.getString(cursor.getColumnIndex(ID));
// 获得联系人姓名:String类型
displayName = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
// 查看联系人有多少个号码,如果没有号码,返回0
int phoneCount = cursor.getInt(cursor.getColumnIndex(HAS_PHONE_NUMBER));
if (!names.contains(displayName)) {
names.add(displayName);
FriendItem item;
item = new FriendItem();
item.setContactId(contactId);
item.setDisplayName(displayName);
item.setPhoneCount(phoneCount);
friendList.add(item);
}
} while (cursor.moveToNext());
cursor.close();
return friendList;
}
//bean对象,存储通讯录的相关字段
public class FriendItem implements Serializable{
private List<String> phoneList;
@Override
public String toString() {
return "FriendItem{" +
"displayName='" + displayName + '\'' +
", contactId='" + contactId + '\'' +
", phoneCount=" + phoneCount +
'}';
}
String displayName;
String contactId;
int phoneCount;
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getContactId() {
return contactId;
}
public void setContactId(String contactId) {
this.contactId = contactId;
}
public int getPhoneCount() {
return phoneCount;
}
public void setPhoneCount(int phoneCount) {
this.phoneCount = phoneCount;
}
public List<String> getPhoneList() {
return phoneList;
}
public void setPhoneNumber(List<String> phoneList) {
this.phoneList=phoneList;
}
}
/**根据cotact_id来获取该联系人的手机号**/
public static FriendItem getPhoneNumberFromContactID(Context context, FriendItem item) {
Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
Cursor phoneCursor;
ContentResolver contentResolver = context.getContentResolver();
List<String> phoneList = new ArrayList<String>();
if (item.getContactId() == null) {
return item;
}
phoneCursor = contentResolver.query(CONTENT_URI, null, CONTACT_ID + "=" + item.getContactId(), null, null);
if (!phoneCursor.moveToFirst()) {
return item;
}
do {
String temp = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
if (temp != null) {
phoneList.add(temp);
}
} while (phoneCursor.moveToNext());
item.setPhoneNumber(phoneList);
return item;
}
//根据cotact_id来获取联系人的名字
public static FriendItem getNameFromContactID(Context context, FriendItem item) {
Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NAME = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
Cursor phoneCursor;
ContentResolver contentResolver = context.getContentResolver();
String name=new String() ;
if (item.getContactId() == null) {
return item;
}
phoneCursor = contentResolver.query(CONTENT_URI, null, CONTACT_ID + "=" + item.getContactId(), null, null);
if (!phoneCursor.moveToFirst()) {
return item;
}
do {
String temp = phoneCursor.getString(phoneCursor.getColumnIndex(NAME));
if (temp != null) {
name=temp;
}
} while (phoneCursor.moveToNext());
item.setDisplayName(name);
return item;
}
/**
*获取联系人头像
**/
public static Bitmap getAddressBitmap(Context context, FriendItem item) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
Long.parseLong(item.getContactId()));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
Bitmap photo = BitmapFactory.decodeStream(input);
return photo;
}
//调用系统的编辑联系人 注:这个方法可能有些手机会编辑完后先弹出系统的界面
Intent editIntent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://com.android.contacts/contacts/" + friendItem.getContactId()));
startActivityForResult(editIntent, EDIT_CONTACTS);
//调用系统添加联系人
Intent addIntent = new Intent(Intent.ACTION_INSERT, Uri.withAppendedPath(Uri.parse("content://com.android.contacts"), "contacts"));
addIntent.setType("vnd.android.cursor.dir/person");
addIntent.setType("vnd.android.cursor.dir/contact");
addIntent.setType("vnd.android.cursor.dir/raw_contact");
// addIntent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, "187102890321321");// 可以自带号码
startActivity(addIntent);
鄙人水平有限,若有错误请不吝指出。