从android联系人选择器获取联系信息 [英] get contact info from android contact picker

查看:28
本文介绍了从android联系人选择器获取联系信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用联系人选择器,将人员姓名、电话和电子邮件转换为字符串,然后使用意图将它们发送到另一个活动.到目前为止,这是有效的:

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);  
        }  
    }  
}

但是如果我加入:

String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

强制关闭

也许还有其他方法可以获取他们的号码?

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 字段.由于可以存储多个电子邮件地址,循环遍历 Cursor 中返回的记录.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆