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

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

一,获取全部联系人并装到集合中
①联系人工具类


/**
 * 获取联系人信息
 */
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();
        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;
    }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值