得到Android的联系人选择联系人信息 [英] get contact info from android contact picker
问题描述
我试图呼叫联系人选择器,获得人的姓名,电话和电子邮件为字符串并使用意图将其发送给另一个活动。到目前为止,这个作品:
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?
推荐答案
电话号码
电话号码存储在自己的表,并需要单独查询。要查询的电话号码表使用存储在SDK变量ContactsContract.CommonDataKinds.Phone.CONTENT_URI的URI。使用WHERE条件来获得的电话号码的指定联系人。
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.
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();
}
执行针对Android的接触SQLite数据库第二个查询。的电话号码查询与存储在ContactsContract.CommonDataKinds.Phone.CONTENT_URI的URI。接触的ID被存储在电话表作为ContactsContract.CommonDataKinds.Phone.CONTACT_ID和WHERE子句用于限制返回的数据。
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.
电子邮件地址
查询电子邮件地址类似于电话号码。查询必须执行从数据库中获取电子邮件地址。查询存储在ContactsContract.CommonDataKinds.Email.CONTENT_URI查询电子邮件地址表中的URI。
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.
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();
由于用手机查询邮件表中的字段名称也存储在ContactsContract.CommonDataKinds。电子邮件查询关于在ContactsContract.CommonDataKinds.Email.CONTENT_URI URI中执行,并且在WHERE子句的ContactsContract.CommonDataKinds.Email.CONTACT_ID字段匹配。由于多个电子邮件地址可以通过光标返回的记录被存储循环。
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.
更多教程这里
此方法需要Android的API版本5或更高。
This method requires Android API version 5 or higher.
这篇关于得到Android的联系人选择联系人信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!