更新时间:2022-10-15 14:57:46
这是可以做到如下:
按钮
点击或点击的EditText
用来收集手机号)//点击后选择联系人
@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(REQ code,结果code,数据);
开关(REQ code){
情况1) :
getContactInfo(数据);
edittext.setText(联系人姓名); //你的EditText设置文本
打破;
}
}
当用户点击这将加载所有接触的EditText
。
公共无效getContactInfo(意向意图){
字符串phoneNumber的= NULL;
字符串名称= NULL; 光标光标= managedQuery(intent.getData(),NULL,NULL,NULL,NULL);
而(cursor.moveToNext()){
字符串的ContactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
名称= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 串hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 如果(hasPhone.equalsIgnoreCase(1))
hasPhone =真;
其他
hasPhone =假; 如果(Boolean.parseBoolean(hasPhone)){
光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+的ContactID,NULL,NULL);
而(phones.moveToNext()){
phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } //结束时
phones.close();
}// 万一
} //结束时
返回ARR;
} //结束类
I'm sending sms to a person by writing his phone number but now I want to add functionality of getting number automatically from phone book contacts by selecting sender's contact.Can anyone give me some idea how to add that functionality in my application ? Thanks in avance.
This can be done as follows:
button
click or on clicking on the EditText
you use to collect phone number)// Pick contacts when clicked
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
getContactInfo(data);
edittext.setText(contactName); // Set text on your EditText
break;
}
}
This will load all the contacts when the user clicks the EditText
.
public void getContactInfo(Intent intent) {
String phoneNumber = null;
String name = null;
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false";
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}// end while
phones.close();
}// end if
}// end while
return arr;
}// end class