如何导入特定联系人的 phone 号码?
How to import a Specific Contact's phone number?
我正在尝试读取使用联系人选择器选择的联系人的 Phone 号码。
显示名称工作正常,但 Phone 号码不工作。
代码:
//calling Contact Picker
public void CPick(View v){
Intent intent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
//Contact Picker here:
protected void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode,resultCode, data);
if (reqCode==PICK_CONTACT){
if(resultCode==AppCompatActivity.RESULT_OK){
Uri contatctData=data.getData();
Cursor c=getContentResolver().query(contatctData,null,null,null,null);
if (c.moveToFirst()){
//String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//Above line works Fine
String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Above line gives error on runtime "invalid column"
Toast.makeText(this,"U have picked:"+name,Toast.LENGTH_SHORT).show();
}
}
}
}
任何帮助将不胜感激,因为我无法在任何地方找到相关答案。
如果您想让用户选择 phone 号码,最好的选择是使用 PHONE-PICKER
而不是 CONTACT-PICKER
:
Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_PHONE);
...
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
Uri phoneUri = intent.getData();
Cursor cur = getContentResolver().query(phoneUri, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
if (cur != null && cur.moveToFirst()){
String name = cur.getString(0);
String number = cur.getString(1);
Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
cur.close();
}
}
}
试试这个方法:
private void retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactId);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactId}, null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
您应该会在 logcat 中看到联系电话。
我正在尝试读取使用联系人选择器选择的联系人的 Phone 号码。 显示名称工作正常,但 Phone 号码不工作。 代码:
//calling Contact Picker
public void CPick(View v){
Intent intent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
//Contact Picker here:
protected void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode,resultCode, data);
if (reqCode==PICK_CONTACT){
if(resultCode==AppCompatActivity.RESULT_OK){
Uri contatctData=data.getData();
Cursor c=getContentResolver().query(contatctData,null,null,null,null);
if (c.moveToFirst()){
//String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//Above line works Fine
String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Above line gives error on runtime "invalid column"
Toast.makeText(this,"U have picked:"+name,Toast.LENGTH_SHORT).show();
}
}
}
}
任何帮助将不胜感激,因为我无法在任何地方找到相关答案。
如果您想让用户选择 phone 号码,最好的选择是使用 PHONE-PICKER
而不是 CONTACT-PICKER
:
Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_PHONE);
...
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
Uri phoneUri = intent.getData();
Cursor cur = getContentResolver().query(phoneUri, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
if (cur != null && cur.moveToFirst()){
String name = cur.getString(0);
String number = cur.getString(1);
Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
cur.close();
}
}
}
试试这个方法:
private void retrieveContactNumber() {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactId);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactId}, null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
您应该会在 logcat 中看到联系电话。