access the contact number of a contact
I am not able to access the contact number of a contact. I got this code from internet
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == PICK_CONTACT)
{
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
cursor.moveToNext();
String conta开发者_JS百科ctId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();
}
But it displays only the name of the contact. I want the contact's phone number.
Check this post out, it has the answer your looking for: Read all contact's phone numbers in android
Modifying your code snippet
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts._ID);
startActivityForResult(intent, PICK_CONTACT);
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{ if (requestCode == PICK_CONTACT)
{ Cursor cursor = managedQuery(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + intent.getData(), null, null);
cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(Email._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(Email.DATA1));
Toast.makeText(this, "Contect LIST = "+name, Toast.LENGTH_LONG).show();
}
// phone numbers
int _contact_id=10111; // your contact id
Cursor curPhone = null;
// 1 = home ,2= personal(mobile), 3= work
try {
Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] PROJECTION_PHONE = new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.CommonDataKinds.Phone.TYPE};
String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
String[] SELECTION_ARRAY_PHONE = new String[]{String.valueOf(_contact_id)};
curPhone = context.getContentResolver()
.query(URI_PHONE, PROJECTION_PHONE, SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
if (curPhone.getCount() > 0) {
curPhone.moveToFirst();
int indexNumber = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int labelIndex = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
int indexPhoneType = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
while (!curPhone.isAfterLast()) {
String contactNumber = curPhone.getString(indexNumber);
String labelName = "";
int labelType = curPhone.getInt(indexPhoneType);
if (labelType ==ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) {
labelName = curPhone.getString(labelIndex);
}
android.util.Log.e("!_@)(@_Number:- ", contactNumber + "[" + labelType + "] " + labelName);
curPhone.moveToNext();
}
} else {
// noPhones = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (curPhone != null) {
curPhone.close();
}
}
To get contact name, number as well as email of a selected contact from your contact list, then simply add this code.
private Button btn;
private String TAG = "Contacts";
private static final int RESULT_PICK_CONTACT = 1;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectSingleContact();
}
});
private void selectSingleContact() {
Intent contactPickerIntent = new
Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForResult
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("ContactFragment", "Failed to pick contact");
}
}
private void contactPicked(Intent data) {
Uri uri = data.getData();
Log.i(TAG, "contactPicked() uri " + uri.toString());
Cursor cursor;
ContentResolver cr = getActivity().getContentResolver();
try {
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (null != cur && cur.getCount() > 0) {
cur.moveToFirst();
for (String column : cur.getColumnNames()) {
Log.i(TAG, "contactPicked() Contacts column " + column + " : " + cur.getString(cur.getColumnIndex(column)));
}
}
if (cur.getCount() > 0) {
//Query the content uri
cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (null != cursor && cursor.getCount() > 0) {
cursor.moveToFirst();
for (String column : cursor.getColumnNames()) {
Log.i(TAG, "contactPicked() uri column " + column + " : " + cursor.getString(cursor.getColumnIndex(column)));
}
}
cursor.moveToFirst();
id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Log.i(TAG, "contactPicked() uri id " + id);
String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Log.i(TAG, "contactPicked() uri contact id " + contact_id);
// column index of the contact name
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// column index of the phone number
phoneNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//get Email id of selected contact....
Log.e("ContactsFragment", "::>> " + id + name + phoneNo);
Cursor cur1 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contact_id}, null);
if (null != cur1 && cur1.getCount() > 0) {
cur1.moveToFirst();
for (String column : cur1.getColumnNames()) {
Log.i(TAG, "contactPicked() Email column " + column + " : " + cur1.getString(cur1.getColumnIndex(column)));
email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
//HERE YOU GET name, phoneno & email of selected contact from contactlist.....
Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
} else {
Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
}
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
Also add permissions to your AndroidManifest.xml It works for me, hope it will works for you also.
精彩评论