如何获取所选联系人的电话号码&联系人名单中的姓名。
答案 0 :(得分:3)
onClick代码
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
pickContact.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE)
startActivityForResult(pickContact, 1);
onActivityResult代码
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
int phoneIndex = getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String num = c.getString(phoneIndex);
Toast.makeText(MainActivity.this, "Number=" + num, Toast.LENGTH_LONG).show();
}
答案 1 :(得分:2)
要从联系人列表中获取所选联系人的联系人姓名,电话号码以及电子邮件,您只需添加此代码即可。
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();
}
}
还为AndroidManifest.xml添加权限 它适用于我,希望它也适合你。
答案 2 :(得分:-1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_usercontact);
class MainActivity extends Activity {
private final int REQUEST_CODE=99;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_usercontact);
EditText editText = (EditText)findViewById(R.id.editTextcontacts1);
EditText editText1 = (EditText)findViewById(R.id.editTextcontacts2);
EditText editText2 = (EditText)findViewById(R.id.editTextcontacts3);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
}
});
editText1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
}
});
editText2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (REQUEST_CODE):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(MainActivity.this, "Number=" + num, Toast.LENGTH_LONG).show();
}
}
}
break;
}
}
}
}
}
}