Android 电话薄调用相关


  /*
     * 根据电话号码取得联系人姓名
     */
    public static String getContactNameByPhoneNumber(Context context, String address) {
        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };

        // 将自己添加到 msPeers 中
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, // Which columns to return.
                ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
                        + address + "'", // WHERE clause.
                null, // WHERE clause value substitution
                null); // Sort order.

        if (cursor == null) {
            Log.d(TAG, "getPeople null");
            return null;
        }
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);

            // 取得联系人名字
            int nameFieldColumnIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String name = cursor.getString(nameFieldColumnIndex);
            return name;
        }
        return null;
    }

/**
     * 获取所有联系人内容
     * @param context
     * @param address
     * @return
     */
    public static String getContacts(Context context) {
        StringBuilder sb = new StringBuilder();
        
        ContentResolver cr = context.getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);

        if (cursor.moveToFirst()) {
            do {
                String contactId = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cursor
                        .getString(cursor
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                //第一条不用换行
                if(sb.length() == 0){
                    sb.append(name);
                }else{
                    sb.append("\n" + name);
                }
                
                Cursor phones = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    String phoneNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // 添加Phone的信息
                    sb.append("\t").append(phoneNumber);
                    
                }
                phones.close();
                
            } while (cursor.moveToNext());
        }
        cursor.close();
        return sb.toString();

-----------------------------权限-------------:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

---------------------直接调用系统电话薄:--------------------

startActivityForResult(new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI), 0);
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        Config.putLog("requestCode:" + requestCode + "resultCode:" + resultCode);
        if (resultCode == Activity.RESULT_OK) {
            // ContentProvider展示数据类似一个单个数据库表
            // ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
            ContentResolver reContentResolverol = getContentResolver();
            // URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
            Uri contactData = data.getData();
            // 查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
            Cursor cursor = managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();
            // 获得DATA表中的名字
            phone_name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            // 条件为联系人ID
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            // 获得DATA表中的电话号码,条件为联系人ID,因为手机号码可能会有多个
            Cursor phone = reContentResolverol.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                            + contactId, null, null);
            if (phone.getCount() > 1) {
                phone_numbers = new String[phone.getCount()];
            }
            int count = 0;
            while (phone.moveToNext()) {
                phone_number = phone
                        .getString(
                                phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                        .replace("+86", "").replace(" ", "");
                Config.putLog("电话号码---" + phone_number);
                if (phone.getCount() > 1) {
                    phone_numbers[count] = phone_number;
                    ++count;
                }
            }
            if (phone.getCount() > 1) {
                Dialog alertDialog = new AlertDialog.Builder(this)
                        .setTitle("请选择电话号码")
                        .setItems(phone_numbers,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        phone_number = phone_numbers[which];
                                        if (Config.isMobileNO(phone_number)) {
                                            et_mobile.setText(phone_number);
                                            tv_phone_name.setText(phone_name);
                                        } else {
                                            tv_ISP.setTextColor(Color.RED);
                                            tv_ISP.setText("号码格式错误");
                                        }
                                    }
                                })
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                    }
                                }).create();
                alertDialog.show();
            } else {
                et_mobile.setText(phone_number);
                tv_phone_name.setText(phone_name);
                if (!Config.isMobileNO(phone_number)) {
                    tv_ISP.setTextColor(Color.RED);
                    tv_ISP.setText("号码格式错误");
                }

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

 

posted @ 2015-01-08 15:55  WisonWang  阅读(155)  评论(0编辑  收藏  举报