您的位置:首页 > 移动开发 > Android开发

Android开发获取联系人信息&根据姓名查找电话%根据电话查找姓名

2017-02-22 16:40 501 查看
最近使用到联系人查找的功能并直接拨号,查看自己之前写的代码,感觉着实麻烦。还是觉得整理成博客比较好,于是就整理了一下。


一,获取全部联系人并装到集合中

①联系人工具类

/**
* 获取联系人信息
*/
public class ContactsEngine {
/**
* 获取系统的联系人信息
*/
public static List<ContactsInfo> getAllContacts(Context context){

List<ContactsInfo> list = new ArrayList<ContactsInfo>();

//获取内容解析者
ContentResolver contentResolver = context.getContentResolver();
Uriuri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
};
Cursor cursor = contentResolver.query(uri, projection, null, null, null);
//解析cursor获取数据
while(cursor.moveToNext()){
String name = cursor.getString(0);
String number = cursor.getString(1);
int contactId = cursor.getInt(2);

ContactsInfo contactsInfo = new ContactsInfo(name, number, contactId);

list.add(contactsInfo);
}
return list;
}

/**
* 根据联系人的id,获取联系人的头像
*/
public static Bitmap getContactPhoto(Context context,int contactid){
ContentResolver contentResolver = context.getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactid+"");
//获取联系人的头像,以流的形式返回
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri);
//将流转化成bitmap
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//关流
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return bitmap;

}


②联系人信息Javabean

/**
* 联系人的bean
*
*/
public class ContactsInfo {

public String name;
public String number;
public int id;

public ContactsInfo(String name, String number, int id) {
super();
b968

this.name = name;
this.number = number;
this.id = id;
}

}


二,根据联系人查找电话号码直接拨打号码

/**
*根据名字拨打电话
*/
public void nameNumberCall(String name) {
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (name.equals(contactName)) {
Cursor phone = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if (phone.moveToNext()) {
String phoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
mContext.startActivity(intentPhone);
return;
}
break;
}
}else {
//TODO 根据姓名没有查找到联系人给用户做一个友好提示
}
}
}


根据电话查找联系人

/**
* 根据电话查找姓名
*/
public String numberToName(String num) {
Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num));
mView.setIntent(intentPhone);
String displayName = null;
Cursor cursor = null;
try {
ContentResolver resolver = mContext.getContentResolver();
Uri uri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI.buildUpon().appendPath(num).build();
String[] projection = new String[]{COLUMN_ID, COLUMN_DISPLAY_NAME};
cursor = resolver.query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndexName = cursor.getColumnIndex(COLUMN_DISPLAY_NAME);
displayName = cursor.getString(columnIndexName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return displayName;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐