android獲取手機(jī)通訊錄聯(lián)系人信息
private void getPhoneContacts() {
ContentResolver resolver = this.getContentResolver();
// 獲取手機(jī)聯(lián)系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME,
Phone.NUMBER },
Phone.DISPLAY_NAME + "=?" + " AND " + Phone.TYPE + "='"
+ Phone.TYPE_MOBILE + "'", new String[] { name }, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
String number = phoneCursor.getString(2);
// 當(dāng)手機(jī)號(hào)碼為空的或者為空字段 跳過當(dāng)前循環(huán)
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到聯(lián)系人名稱
String username = phoneCursor.getString(1);
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
}
phoneCursor.close();
}
}
獲得手機(jī)sim卡聯(lián)系人信息
sim卡和手機(jī)本人 獲取的方式類似 只是url有點(diǎn)不一樣 ,須要注意的一點(diǎn)是 sim卡 是沒有聯(lián)系人頭像的。
private void getSIMContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 獲取Sims卡聯(lián)系人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri,
new String[] { Phone.CONTACT_ID, Phone.DISPLAY_NAME,
Phone.NUMBER },
Phone.DISPLAY_NAME + "=?" + " AND " + Phone.TYPE + "='"
+ Phone.TYPE_MOBILE + "'", new String[] { name }, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
String number = phoneCursor.getString(2);
// 當(dāng)手機(jī)號(hào)碼為空的或者為空字段 跳過當(dāng)前循環(huán)
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到聯(lián)系人名稱
String username = phoneCursor.getString(1);
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
}
phoneCursor.close();
}
}
調(diào)用系統(tǒng)撥打電話的界面 ,代碼如下。
tel:電話號(hào)碼
//調(diào)用系統(tǒng)方法撥打電話
Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mContactsNumber.get(position)));
startActivity(dialIntent);
最后,千萬(wàn)別忘記在AndroidManifest.xml文件中添加權(quán)限,否則運(yùn)行程序是報(bào)錯(cuò)!
<!-- 讀取聯(lián)系人權(quán)限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- 撥打電話權(quán)限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>