进入系统联系人页面代码段
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,Int requestCode);
需要添加的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
返回处理,获取联系人姓名和电话类:
package com.mystudy.kibi.utils;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
/**
* Created by KIBI熊 on 16/7/1.
*/
public class GetContactMsgUtil {
private String name;
private String phone;
public GetContactMsgUtil(Context context, Intent data){
super();
Uri contactData = data.getData();
CursorLoader cursorLoader = new CursorLoader(context,contactData,null,null,null,null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor.moveToFirst()){
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
phone = "此联系人暂未存入号码";
Cursor phones = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null,
null);
if (phones.moveToFirst()) {
phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
cursor.close();
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
(。´・(・ω・)ノ(・ω・)ノ。) 使用实例