Android 保存號(hào)碼至已有聯(lián)系人,新建聯(lián)系人,讀取手機(jī)聯(lián)系人

添加必要的權(quán)限

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

//添加到手機(jī)聯(lián)系人
    public static void addContact(Context context,String name,String phone){
        Uri insertUri = android.provider.ContactsContract.Contacts.CONTENT_URI;
        Intent intent = new Intent(Intent.ACTION_INSERT, insertUri);
        intent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, name);//名字顯示在名字框
        intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE,phone);//號(hào)碼顯示在號(hào)碼框
//        intent.putExtra(ContactsContract.Intents.Insert.POSTAL,"");//地址顯示在地址框
        context.startActivity(intent);
    }

//保存至已有聯(lián)系人
    public static void saveExistContact(Context context,String name, String phone) {
        Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
        intent.setType("vnd.android.cursor.item/person");
        intent.setType("vnd.android.cursor.item/contact");
        intent.setType("vnd.android.cursor.item/raw_contact");
        //    intent.putExtra(android.provider.ContactsContract.Intents.Insert.NAME, name);
        intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, phone);
        intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE_TYPE, 3);
        context.startActivity(intent);
    }


//讀取手機(jī)聯(lián)系人列表
public static List<HashMap<String, String>> getAllContactInfo() {
        SystemClock.sleep(3000);
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        // 1.獲取內(nèi)容解析者
        ContentResolver resolver = Utils.getContext().getContentResolver();
        // 2.獲取內(nèi)容提供者的地址:com.android.contacts
        // raw_contacts表的地址 :raw_contacts
        // view_data表的地址 : data
        // 3.生成查詢地址
        Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri date_uri = Uri.parse("content://com.android.contacts/data");
        // 4.查詢操作,先查詢r(jià)aw_contacts,查詢contact_id
        // projection : 查詢的字段
        Cursor cursor = resolver.query(raw_uri, new String[]{"contact_id"},
                null, null, null);
        // 5.解析cursor
        while (cursor.moveToNext()) {
            // 6.獲取查詢的數(shù)據(jù)
            String contact_id = cursor.getString(0);
            // cursor.getString(cursor.getColumnIndex("contact_id"));//getColumnIndex
            // : 查詢字段在cursor中索引值,一般都是用在查詢字段比較多的時(shí)候
            // 判斷contact_id是否為空
            if (!StringUtils.isEmpty(contact_id)) {//null   ""
                // 7.根據(jù)contact_id查詢view_data表中的數(shù)據(jù)
                // selection : 查詢條件
                // selectionArgs :查詢條件的參數(shù)
                // sortOrder : 排序
                // 空指針: 1.null.方法 2.參數(shù)為null
                Cursor c = resolver.query(date_uri, new String[]{"data1",
                                "mimetype"}, "raw_contact_id=?",
                        new String[]{contact_id}, null);
                HashMap<String, String> map = new HashMap<String, String>();
                // 8.解析c
                while (c.moveToNext()) {
                    // 9.獲取數(shù)據(jù)
                    String data1 = c.getString(0);
                    String mimetype = c.getString(1);
                    // 10.根據(jù)類型去判斷獲取的data1數(shù)據(jù)并保存
                    if (mimetype.equals("vnd.android.cursor.item/phone_v2")) {
                        // 電話
                        map.put("phone", data1);
                    } else if (mimetype.equals("vnd.android.cursor.item/name")) {
                        // 姓名
                        map.put("name", data1);
                    }
                }
                // 11.添加到集合中數(shù)據(jù)
                list.add(map);
                // 12.關(guān)閉cursor
                c.close();
            }
        }
        // 12.關(guān)閉cursor
        cursor.close();
        return list;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容