Android獲取手機聯(lián)系人的方法
更新時間:2017年09月17日 10:21:47 投稿:lqh
這篇文章主要介紹了Android 獲取系統(tǒng)聯(lián)系人信息的實例的相關資料,希望通過本文大家能實現(xiàn)這樣的功能,需要的朋友可以參考下
Android 獲取系統(tǒng)聯(lián)系人信息的實例
一、獲取手機聯(lián)系人姓名及手機號
//跳轉到系統(tǒng)聯(lián)系人應用 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); try { startActivityForResult(intent, Contacts1RequestCode); } catch (Exception e) { LogManager.e("打開聯(lián)系人信息失敗"); }
添加權限申請
<uses-permission android:name="android.permission.READ_CONTACTS" />
選擇聯(lián)系人并返回
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (Contacts1RequestCode == requestCode) {// 取聯(lián)系信息返回 if (resultCode == RESULT_OK) { Uri contactData = data.getData(); Cursor cursor = getContentResolver().query(contactData, null, null, null, null); //Key聯(lián)系人姓名,Value聯(lián)系人手機號 Map<String, String> phoneMap = this.getContactPhone(cursor); if (!cursor.isClosed()) { cursor.close(); } if (null != phoneMap && !phoneMap.isEmpty()) { Set<String> keySet = phoneMap.keySet(); if (null != keySet && !keySet.isEmpty()) { Object[] keys = keySet.toArray(); String phoneName = (String) keys[0]; String phoneNo = phoneMap.get(phoneName); } } } } }
/** * 獲取聯(lián)系人姓名及手機號 * * @param cursor * @return Key為聯(lián)系人姓名,Value為聯(lián)系人手機號 */ private Map<String, String> getContactPhone(Cursor cursor) { Map<String, String> resultMap = new HashMap<String, String>(); String phoneName = null;// 姓名 String mobilePhoneNo = null;// 手機號 if (null != cursor) { cursor.moveToFirst(); // 獲得聯(lián)系人的ID號 int idFieldIndex = cursor .getColumnIndex(ContactsContract.Contacts._ID); String contactId = cursor.getString(idFieldIndex); // 聯(lián)系人姓名 int idphoneNameIndex = cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); phoneName = cursor.getString(idphoneNameIndex); // 獲得聯(lián)系人的電話號碼的cursor; Cursor allPhones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { contactId }, null); // 所以聯(lián)系電話(包話電話和手機號) List<String> allPhoneNumList = new ArrayList<String>(); if (allPhones.moveToFirst()) { // 遍歷所有的電話號碼 for (; !allPhones.isAfterLast(); allPhones.moveToNext()) { int telNoTypeIndex = allPhones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); int telNoType = allPhones.getInt(telNoTypeIndex); int telNoIndex = allPhones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String telNo = allPhones.getString(telNoIndex); allPhoneNumList.add(telNo); if (2 == telNoType) {// 手機號(原生態(tài)的SDK定義:mobile是2,home是1,work是3,other是7) mobilePhoneNo = telNo; break; } } if (!allPhones.isClosed()) { allPhones.close(); } if (null == mobilePhoneNo) {// 沒有存貯手機號 if (!allPhoneNumList.isEmpty()) {// 存在其它號碼 for (String tel : allPhoneNumList) { if (VerifyKit.isLegal(FormatType.MobilePhone, tel)) {// 取屬于手機號格式 mobilePhoneNo = tel; break; } } } } } if (!cursor.isClosed()) { cursor.close(); } resultMap.put(phoneName, mobilePhoneNo); } return resultMap; }
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Android 實現(xiàn)圖片生成卷角和圓角縮略圖的方法
本篇文章主要介紹了Android 實現(xiàn)圖片生成卷角和圓角縮略圖的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11Android實現(xiàn)第三方授權登錄、分享以及獲取用戶資料
本篇文章介紹了Android實現(xiàn)第三方授權登錄、分享以及獲取用戶資料,詳細的介紹了第三方授權登錄的實現(xiàn)代碼,有需要的朋友可以了解一下。2016-11-11詳解Android中Activity運行時屏幕方向與顯示方式
本文主要對如何控制Android中Activity運行時屏幕方向與顯示方式進行詳細全面的實例講解。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12Android基于service實現(xiàn)音樂的后臺播放功能示例
這篇文章主要介紹了Android基于service實現(xiàn)音樂的后臺播放功能,結合實例形式分析了Android基于Service組件實現(xiàn)多媒體音頻播放功能的步驟與相關操作技巧,需要的朋友可以參考下2016-10-10