从 Intent.ACTION_PICK 返回的 URI 中获取特定联系信息 [英] Get specific contact information from URI returned from Intent.ACTION_PICK

查看:32
本文介绍了从 Intent.ACTION_PICK 返回的 URI 中获取特定联系信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Android 应用,它的数据类型代表一个人(特别是孩子的父母或监护人).我希望能够从 Android 设备的联系人数据库中导入"相关数据字段.(这应该是可选的;也就是说,不需要父母/监护人已经在联系人数据库中,如果他们添加新的父母/监护人,联系人数据库也不会更新.)

I am writing an Android app that has a data type that represents a person (specifically, the parent or guardian of a child). I'd like to be able to "import" the relevant data fields from the Contacts database in the Android device. (This should be optional; that is, it will not be a requirement that the parent/guardian is already in the Contacts database, nor will the Contacts database be updated if they add new parents/guardians.)

到目前为止,我已经编写了代码来启动一个新的 Intent 来选择特定的联系人(使用 Intent.ACTION_PICK).然后我得到一个代表数据库中特定联系人的 URI.

So far, I have written code to start a new Intent to choose the specific Contact (using Intent.ACTION_PICK). I then get a URI that represents a specific Contact in the database.

不幸的是,我不知道下一步是什么.看起来这应该是世界上最简单的事情,但显然不是.我已经通读了 Android 开发者网站上的文档,并且阅读了不止一本 Android 书籍.没有快乐.

Unfortunately, I don't know what the next step is. It seems like this should be the simplest thing in the world to do, but apparently not. I've read through the documentation on the Android developer website, and I've looked through more than one Android book. No joy.

我想得到的具体信息是:

The specific information I'd like to get, is:

  1. 联系人姓名(如果可能,名字和姓氏分开)

  1. The contact's name (first and last separately if possible)

联系人的(主要)电子邮件地址

The contact's (primary) email address

联系人的手机号码

我想这应该可以通过使用 ContentResolver 进行查询,但我不知道如何使用从 Intent 返回的 URI 来做到这一点.大多数文档都假设您拥有联系人 ID,而不是联系人的 URI.此外,我不知道我可以将什么样的字段放入查询的投影中,假设这甚至是做我想做的事情的正确方法.

I imagine that this should be possible by querying using the ContentResolver, but I have no idea how to do this with the URI returned from the Intent. Most of the documentation assumes you have the Contact ID, not the Contact's URI. Also, I have no idea what kind of fields I can put into the projection for the query, assuming that this is even the right way to do what I want.

这是我的起始代码:

// In a button's onClick event handler:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

// In onActivityResult:
if (resultCode == RESULT_OK) {
    if (requestCode == PICK_CONTACT) {
        contactURI = data.getData();
        // NOW WHAT?
    }
}

推荐答案

事实证明,有更好的方法来做到这一点.

As it turns out, there is a better way to do this.

正如我所提到的,ContactsContract.Contacts.Entity 类在 API 11 之前不可用.但是,ContactsContract.Data 类早在 API 5 中就可用了,您可以像使用 Entity 类一样使用该类.

As I mentioned, the ContactsContract.Contacts.Entity class was not available until API 11. However, the ContactsContract.Data class was available way back in API 5, and you can use that class in largely the same way that you use the Entity class.

我已经更新了我的代码.它与 Entity 类的代码非常相似,工作方式大致相同.但是,我已经在运行 Gingerbread 的手机上对其进行了测试,效果很好.

I've updated my code. It is very similar to the code for the Entity class, and works largely the same way. However, I've tested it with my phone running Gingerbread, and it works just fine.

我必须做的一个更改是:似乎没有办法从初始查询中获取 ContactsContract.Data.RAW_CONTACT_ID,而该 ID not 与您从 eg 中获得的 ID 相同ContactsContract.Contacts._ID.相反,我查询了 ContactsContract.Contacts.DISPLAY_NAME 常量,它几乎在每个 ContactsContract 类中都是一致的.

One change I had to make is this: There doesn't seem to be a way to get the ContactsContract.Data.RAW_CONTACT_ID from the initial query, and that ID is not the same as the ID you get from e.g. ContactsContract.Contacts._ID. Instead, I queried on the ContactsContract.Contacts.DISPLAY_NAMEconstant, which is consistent across pretty much every ContactsContract class.

这是工作代码:

        Cursor cursor;  // Cursor object
        String mime;    // MIME type
        int dataIdx;    // Index of DATA1 column
        int mimeIdx;    // Index of MIMETYPE column
        int nameIdx;    // Index of DISPLAY_NAME column

        // Get the name
        cursor = getContentResolver().query(params[0],
                new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                null, null, null);
        if (cursor.moveToFirst()) {
            nameIdx = cursor.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME);
            name = cursor.getString(nameIdx);

            // Set up the projection
            String[] projection = {
                    ContactsContract.Data.DISPLAY_NAME,
                    ContactsContract.Contacts.Data.DATA1,
                    ContactsContract.Contacts.Data.MIMETYPE };

            // Query ContactsContract.Data
            cursor = getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI, projection,
                    ContactsContract.Data.DISPLAY_NAME + " = ?",
                    new String[] { name },
                    null);

            if (cursor.moveToFirst()) {
                // Get the indexes of the MIME type and data
                mimeIdx = cursor.getColumnIndex(
                        ContactsContract.Contacts.Data.MIMETYPE);
                dataIdx = cursor.getColumnIndex(
                        ContactsContract.Contacts.Data.DATA1);

                // Match the data to the MIME type, store in variables
                do {
                    mime = cursor.getString(mimeIdx);
                    if (ContactsContract.CommonDataKinds.Email
                            .CONTENT_ITEM_TYPE.equalsIgnoreCase(mime)) {
                        email = cursor.getString(dataIdx);
                    }
                    if (ContactsContract.CommonDataKinds.Phone
                            .CONTENT_ITEM_TYPE.equalsIgnoreCase(mime)) {
                        phone = cursor.getString(dataIdx);
                        phone = PhoneNumberUtils.formatNumber(phone);
                    }
                } while (cursor.moveToNext());
            }
        }

这篇关于从 Intent.ACTION_PICK 返回的 URI 中获取特定联系信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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