当前位置: 代码迷 >> Android >> 怎么调用android通讯录
  详细解决方案

怎么调用android通讯录

热度:81   发布时间:2016-04-28 05:45:23.0
如何调用android通讯录?

public void inport(View view) {

Intent intentContact = new Intent(Intent.ACTION_PICK,

ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(intentContact, PICK_CONTACT);

}

?

@Override

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

?

if (requestCode == PICK_CONTACT) {

getContactInfo(intent);

?

}

}

?

protected void getContactInfo(Intent intent) {

?

Cursor cursor = managedQuery(intent.getData(), null, null, null, null);

while (cursor.moveToNext()) {

String contactId = cursor.getString(cursor

.getColumnIndex(ContactsContract.Contacts._ID));

String name = cursor

.getString(cursor

.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

?

String hasPhone = cursor

.getString(cursor

.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

?

if (hasPhone.equalsIgnoreCase("1"))

hasPhone = "true";

else

hasPhone = "false";

?

String phoneNumber = null;

?

if (Boolean.parseBoolean(hasPhone)) {

Cursor phones = getContentResolver().query(

ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Phone.CONTACT_ID

+ " = " + contactId, null, null);

while (phones.moveToNext()) {

phoneNumber = phones

.getString(phones

.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}

phones.close();

}

?

Cursor emails = getContentResolver().query(

ContactsContract.CommonDataKinds.Email.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "

+ contactId, null, null);

String emailAddress = null;

while (emails.moveToNext()) {

emailAddress = emails

.getString(emails

.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

}

emails.close();

?

Cursor address = getContentResolver()

.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,

null,

ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID

+ " = " + contactId, null, null);

while (address.moveToNext()) {

?

String poBox = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));

String street = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));

String city = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));

String state = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));

String postalCode = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));

String country = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));

String type = address

.getString(address

.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

}

?

Log.e("联系人", name + ":" + phoneNumber + " ? ? " + emailAddress);

}

cursor.close();

}

  相关解决方案