如何从联系人中获取电子邮件和姓氏?
Posted
技术标签:
【中文标题】如何从联系人中获取电子邮件和姓氏?【英文标题】:How to get email and last name from contacts? 【发布时间】:2016-10-25 16:14:42 【问题描述】:我使用了联系人选择器的意图。我正在通过意图获取联系数据。为此,我正在获取联系人的姓名、电话号码。
当我尝试获取联系人的电子邮件 ID 时,它只显示联系人的号码而不是电子邮件 ID。同样对于联系人的姓氏,尽管最后一个已设置为联系人,但它始终显示为 null。
代码:
private void contactPicked(Intent data)
Cursor cursor = null;
try
String phoneNo = null ;
String name = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int contactIdIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
int lastNameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
mOrganizerPhone = cursor.getString(phoneIndex);
mOraganizerName = cursor.getString(nameIndex);
mOrganizersId = cursor.getString(contactIdIndex);
mOrganizersEmail = cursor.getString(emailIndex);
mOrganizersLastName = cursor.getString(lastNameIndex);
Toast.makeText(PlanEventActivity.this,name+" "+phoneNo,Toast.LENGTH_SHORT).show();
catch (Exception e)
e.printStackTrace();
谢谢你..
【问题讨论】:
我认为这对你有帮助:***.com/questions/12109391/… 您能告诉我要使用的查询吗?因为当我搜索时,我看到了用于获取电子邮件和结构化姓氏的不同查询。 @HuyN 【参考方案1】:使用“id”,你可以得到这样的电子邮件地址,这是一个例子:
public class CustomerForm extends Activity
private final static int CONTACT_PICKER = 1;
private EditText txtMailContacto;
private EditText txtNombreContacto;
private EditText txtTelefono;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_form);
txtMailContacto = (EditText) findViewById(R.id.txtMailContacto);
txtTelefono = (EditText) findViewById(R.id.txtTelefono);
txtNombreContacto = (EditText) findViewById(R.id.txtNombreContacto);
public void pickContact(View v)
Intent contactPickerIntent =
new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
// check whether the result is ok
if (resultCode == RESULT_OK)
// Check for the request code, we might be using multiple startActivityForReslut
switch (requestCode)
case CONTACT_PICKER:
contactPicked(data);
break;
else
Log.e("MainActivity", "Failed to pick contact");
private void contactPicked(Intent data)
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cur.moveToFirst();
try
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cur = getContentResolver().query(uri, null, null, null, null);
cur.moveToFirst();
// column index of the contact ID
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// column index of the contact name
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
txtNombreContacto.setText(name); //print data
// column index of the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]id, null);
while (pCur.moveToNext())
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
txtTelefono.setText(phone); //print data
pCur.close();
// column index of the email
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]id, null);
while (emailCur.moveToNext())
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
txtMailContacto.setText(email); //print data
emailCur.close();
catch (Exception e)
e.printStackTrace();
【讨论】:
对于每个 id 我需要为电子邮件创建光标?那么姓氏呢? @Huy N 没错!关于姓氏,我上面的解决方案使用“ContactsContract.Contacts.DISPLAY_NAME”并获得全名,您可以在 Java 中使用 spit 方法来获得您想要的名称:) 我试过这个解决方案没有收到电子邮件 ID。 @Huy N 它不会进入电子邮件光标的while循环。 @Huy N 是的,我有权限。我可以看到 id 和显示名称,但看不到 emailid。 @Huy N以上是关于如何从联系人中获取电子邮件和姓氏?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Android 中的 Intent 中获取联系信息?