Android开发获取联系人信息根据姓名查找电话%根据电话查找姓名
最近使用到联系人查找的功能并直接拨号,查看自己之前写的代码,感觉着实麻烦。还是觉得整理成博客比较好,于是就整理了一下。
一,获取全部联系人并装到集合中
①联系人工具类
/*** 获取联系人信息*/
public class ContactsEngine {/*** 获取系统的联系人信息*/public static List getAllContacts(Context context){List list = new ArrayList();//获取内容解析者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);//将流转化成bitmapBitmap bitmap = BitmapFactory.decodeStream(inputStream);//关流if (inputStream != null) {try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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();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;}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
