/**
* 获取联系人的信息主要是 数据库的表:row_contacts,data,mimetypes 通过row_contacts查询出联系人的ID
* 通过ID值去data表查询信息 mimetypes表主要是记录了data表中值的属性(电话号码,姓名....)
*/
public class ContactsUtil {
/**
* 根据号码获取联系人
*
* @param number
* 联系人的号码 权限 <uses-permission
* android:name="android.permission.READ_CONTACTS"/>
*/
public static String getContactNameByNumber(Context context, String number) {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(lookupUri,
new String[] { Phone.DISPLAY_NAME }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(cursor
.getColumnIndex(Phone.DISPLAY_NAME));
cursor.close();
return name;
}
return null;
}
/**
* 根据号码获取 联系人的头像
*
* @param number
* 联系人的号码 权限 <uses-permission
* android:name="android.permission.READ_CONTACTS"/>
*/
public static Bitmap getContactIcon(Context context, String number) {
// 先根据联系人的号码去获取联系人的ID
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(lookupUri, new String[] { Phone._ID },
null, null, null);
if (cursor != null && cursor.moveToFirst()) {
long id = cursor.getLong(cursor.getColumnIndex(Phone._ID));
cursor.close();
// 根据ID再去获取图像
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
id);
InputStream inputStream = Contacts.openContactPhotoInputStream(
resolver, contactUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
return null;
}
/**
* 根据指定的Uri 获取联系人的ID值
*/
public static int getContactID(Context context, Uri uri) {
Cursor cursor = context.getContentResolver().query(uri,
new String[] { "has_phone_number", "_id" }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
if (cursor.getInt(0) == 1) {
// cursor.getInt(0)==1说明该联系人有电话号码
int id = cursor.getInt(1);
cursor.close();
return id;
}
}
return -1;
}
/**
* 根据指定的ID值 获取联系人的电话号码
*/
public static String getContactNumberByID(Context context, int contact_id) {
String selection = "contact_id = ?";
String[] selectionArgs = new String[] { String.valueOf(contact_id) };
Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone.NUMBER }, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
String number = cursor.getString(0);
return number;
}
return null;
}
/**
* 查询所有的联系人的姓名和电话号码 Map<String, String> =====>Map<"姓名", "电话号码">
*/
public static List<Map<String, String>> getContactInfo(Context context) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
// 查询联系人
ContentResolver resolver = context.getContentResolver();
Uri uriRaw_contacts = Uri
.parse("content://com.android.contacts/raw_contacts");
Uri uriData = Uri.parse("content://com.android.contacts/data");
// 查选所用用户的信息在数据库中的ID值
Cursor cursor = resolver.query(uriRaw_contacts,
new String[] { "contact_id" }, null, null, null);
// 根据ID值去查询每一个联系人的具体信息
while (cursor.moveToNext()) {
String contact_id = cursor.getString(0);
if (contact_id != null) {
HashMap<String, String> map = new HashMap<String, String>();
// 去data表查询具体信息
Cursor c = resolver.query(uriData, new String[] { "data1",
"mimetype" }, "contact_id=?",
new String[] { contact_id }, null);
while (c.moveToNext()) {
String data1 = c.getString(0);
String mimetype = c.getString(1);
// vnd.android.cursor.item/phone_v2
// vnd.android.cursor.item/name
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
// data1 是电话号码
map.put("phoneNumber", data1);
} else if ("vnd.android.cursor.item/name".equals(mimetype)) {
// data1是姓名
map.put("name", data1);
}
}
if (!TextUtils.isEmpty(map.get("name"))
&& !TextUtils.isEmpty(map.get("phoneNumber"))) {
list.add(map);
}
c.close();
}
}
cursor.close();
return list;
}
}