从android联系人选择器获取联系信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3044545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get contact info from android contact picker
提问by ng93
I'm trying to call the contact picker, get the persons name, phone and e-mail into strings and send them to another activity using an intent. So far this works:
我正在尝试调用联系人选择器,将人员姓名、电话和电子邮件转换为字符串,然后使用意图将它们发送到另一个活动。到目前为止,这是有效的:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
// ...
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("name", name);
startActivityForResult(intent, 0);
}
}
}
But if i add in:
但如果我添加:
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it force closes
它强制关闭
Maybe theres another way to get their number?
也许有另一种方式来获取他们的号码?
回答by Pentium10
Phone Numbers
电话号码
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
电话号码存储在自己的表中,需要单独查询。要查询电话号码表,请使用存储在 SDK 变量 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 中的 URI。使用 WHERE 条件获取指定联系人的电话号码。
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
}
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.
对 Android 联系人 SQLite 数据库执行第二次查询。根据存储在 ContactsContract.CommonDataKinds.Phone.CONTENT_URI 中的 URI 查询电话号码。联系人 ID 作为 ContactsContract.CommonDataKinds.Phone.CONTACT_ID 存储在电话表中,WHERE 子句用于限制返回的数据。
Email Addresses
电子邮件地址
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.
查询电子邮件地址类似于电话号码。必须执行查询才能从数据库中获取电子邮件地址。查询 ContactsContract.CommonDataKinds.Email.CONTENT_URI 中存储的 URI 以查询电子邮件地址表。
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.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.
与电话查询一样,电子邮件表的字段名称也存储在 ContactsContract.CommonDataKinds 下。电子邮件查询在 ContactsContract.CommonDataKinds.Email.CONTENT_URI 中的 URI 上执行,并且 WHERE 子句必须匹配 ContactsContract.CommonDataKinds.Email.CONTACT_ID 字段。由于可以通过 Cursor 中返回的记录循环存储多个电子邮件地址。
More tutorials here
更多教程在这里
This method requires Android API version 5 or higher.
此方法需要 Android API 版本 5 或更高版本。
回答by caller9
Building on the accepted answer, if you want to jump straight to the desired email address and not require the contacts permission use something like this:
基于接受的答案,如果您想直接跳转到所需的电子邮件地址并且不需要联系人权限,请使用以下内容:
private static final int REQUEST_CODE_EMAIL = 1;
void startSelectingEmail() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_EMAIL);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_EMAIL) {
Uri emailUri = data.getData();
Cursor emailCursor = getContext().getContentResolver().query(emailUri, null, null, null, null);
if (emailCursor != null) {
if (emailCursor.moveToFirst()) {
String email = emailCursor.getString(
emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCursor.getString(
emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.TYPE));
Log.d(TAG, "Email: " + emailType + " " + email);
}
emailCursor.close();
}
}
}
This doesn't require the contacts permission to read the email address like the double query methods above. It also makes it so that you do not need to write UI for the user to select the appropriate email address for contacts with multiple emails, the user selects a specific email in the Contacts app so you only get one result.
这不需要联系人权限来读取电子邮件地址,就像上面的双重查询方法一样。它还使您无需为用户编写 UI 来为具有多封电子邮件的联系人选择适当的电子邮件地址,用户在联系人应用程序中选择特定的电子邮件,因此您只会得到一个结果。
The cursor comes back with quite a few columns in addition to just email address like display name, though that has only been verified on a Nexus 5 running Android M.
除了像显示名称这样的电子邮件地址之外,光标还带有相当多的列,尽管这仅在运行 Android M 的 Nexus 5 上得到验证。