1、权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
2、java代码
package com.bruce.testgetcontact;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
private String TAG = "tag";
private ArrayList<Contract> mContractList = new ArrayList<Contract>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
testReadAllContacts();
}
});
}
/*
* 读取联系人的信息
*/
public void testReadAllContacts() {
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
int contactIdIndex = 0;
int nameIndex = 0;
if (cursor.getCount() > 0) {
contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
} else {
Toast.makeText(this, "联系人为空!", Toast.LENGTH_SHORT).show();
}
Contract contract;
while (cursor.moveToNext()) {
String contactId = cursor.getString(contactIdIndex);
String name = cursor.getString(nameIndex);
Log.i(TAG, contactId);
Log.i(TAG, name);
/*
* 查找该联系人的phone信息
*/
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null, null);
int phoneIndex = 0;
// int phoneName = 0;
if (phones.getCount() > 0) {
phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// phoneName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
}
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phoneIndex);
// String phoneNames = phones.getString(phoneName);
//电话号码
Log.i(TAG, phoneNumber);
contract = new Contract();
contract.setName(name);
contract.setPhone(phoneNumber);
mContractList.add(contract);
}
/*
* 查找该联系人的email信息
*/
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
null, null);
int emailIndex = 0;
if (emails.getCount() > 0) {
emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
while (emails.moveToNext()) {
String email = emails.getString(emailIndex);
Log.i(TAG, email);
}
}
}
public class Contract {
private String name;
private String phone;
public Contract() {
}
public Contract(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
}