-2
我想在android中獲取電話號碼作爲字符串,我成功地獲得了聯繫人並從他那裏獲取電話號碼,但日誌中的結果是data1,號碼是32821我不明白我的問題。如何從android中的聯繫人獲取電話號碼
這裏是我的代碼:
public void getContact(View view){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// for ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 10);
// return;
}
Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
this.pickContact = 1;
startActivityForResult(contactsIntent, this.pickContact);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
if(reqCode == this.pickContact){
if (resultCode == Activity.RESULT_OK) {
Log.d("ContactsH", "ResOK");
Uri contactData = data.getData();
Cursor contact = getContentResolver().query(contactData, null, null, null, null);
if (contact.moveToFirst()) {
// String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = ContactsContract.CommonDataKinds.Phone.NUMBER;
// TODO Whatever you want to do with the selected contact name and phone number.
Log.d("ContactsH", "Calling to:"+phoneNumber);
contact.close();
this.callByNumber(phoneNumber);
}
}
}else{
Log.d("ContactsH", "Canceled");
}
}
你能幫助我嗎?
的可能的複製[如何獲得接觸在Android手機號碼(https://stackoverflow.com/questions/11218845/how-to-get-contacts Android的電話號碼 –
'String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));'不給你名字?你有沒有爲數字嘗試同樣的事情? –
我很抱歉,但這不適合我,因爲它顯示相同的號碼,無論我選擇誰。 –