【发布时间】:2020-01-10 00:12:43
【问题描述】:
我尝试了几乎所有其他答案或代码,但我的问题仍然存在。检索到的联系人电话号码是任意 3-4 位数字,而不是联系人的确切电话号码。当我继续拨打它拨打“6855”的号码时。请帮忙。
代码
if(mWasButtonClicked){
String[] projections = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
String selectionClause = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
String[] selectionArgs = new String[] {mContactID};
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projections,selectionClause,selectionArgs,null,null);
try {
if(c.getCount() == 0){return;}
if(c!=null){
while(c.moveToNext()){
c.moveToFirst();
phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("TEST",phoneNumber);
}
}
}finally {
c.close();
}
}
通话按钮
mCallButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("tel:"+phoneNumber);
startActivity(new Intent(Intent.ACTION_CALL,uri));
}
});
用于检索联系人 ID
Uri content_uri = data.getData();
String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID};
Cursor c = getContentResolver().query(content_uri,projection,null,null,null,null);
try {
if(c.getCount() == 0){return;}
if(c!=null){
while(c.moveToNext()){
c.moveToFirst();
String name = c.getString(0);
mGetNameButton.setText(name);
mContactID = c.getString(1);
Log.d("TEST",mContactID);
}
}
}finally {
c.close();
}
和日志
2020-01-10 05:41:38.284 27904-27904/com.example.contactsuri D/TEST: 84
【问题讨论】:
-
这能回答你的问题吗? How to get contacts' phone number in Android
标签: android android-cursor android-contentresolver