我正在尝试将edittext输入用户选择的电话号码。我有一个按钮: -
contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
Phone.CONTENT_URI);
startActivityForResult(intent, 0);
}
});
和以下功能:
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
Cursor c = getContentResolver().query(Phone.CONTENT_URI, null,
null, null, null);
c.moveToFirst();
String phone = c.getString(c.getColumnIndexOrThrow(Phone.NUMBER));
phone = phone.replace("-", "");
Log.v("getting phone number", "Phone Number: " + phone);
txtPhoneNo.setText(phone);
}
break;
}
}
我收到了最后一次联系的电话号码。如何获取所选的电话号码?
答案 0 :(得分:1)
做类似的事情:
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor curContact = managedQuery(contactData, null, null, null, null);
if ((curContact != null) && (curContact.moveToFirst()))
{
String id = curContact.getString(curContact
.getColumnIndexOrThrow(BaseColumns._ID));
// check there's a phone number at all
if (Integer.parseInt(curContact.getString(curContact
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// get the phone number
Cursor curNumbers = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { id }, null);
if ((curNumbers != null) && curNumbers.moveToFirst())
{
String strNumber =
curNumbers.getString(
curNumbers.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER)));
curNumbers.close();
}
}
curContact.close();
}
答案 1 :(得分:0)
看看这段代码!
btn_existing_contacts.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent_contacts = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
/*contacts.setAction(android.content.Intent.ACTION_VIEW);
contacts.setData(People.CONTENT_URI);*/
startActivityForResult(intent_contacts, 0);
//displayContacts();
}
});
从联系人屏幕返回后
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String name,mailid,id;
switch(requestCode)
{
case 0:
{
if(resultCode == RESULT_OK )
{
Uri contactdata = data.getData();
Cursor cur = managedQuery(contactdata, null, null, null, null);
if(cur.moveToFirst())
{
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",new String[]{id}, null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
name = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//mailid = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//mailid = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID));
//Toast.makeText(context_contact, "Name:"+name+"\nmailid:"+email, Toast.LENGTH_SHORT).show();
Intent intent_add_invitees = new Intent(<ClassContext>,<ur classname>.class);
intent_add_invitees.putExtra("invitee_name", name);
intent_add_invitees.putExtra("invitee_mailid", email);
setResult(RESULT_OK, intent_add_invitees);
finish();
}
}
}
}
}