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

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

问题描述

我写一个Android应用程序,有一个重新presents的人(特别是父母或儿童的监护人)的数据类型。我希望能进口的相关数据从联系人数据库中的Andr​​oid设备领域。 (这应该是可选的;也就是说,它不会是一个要求,即家长/监护人已经在联系人数据库,也不如果他们添加新的家长/监护人将联系人数据库进行更新)

到目前为止,我已经写了code,开始一个新的意图来选择具体的联系方式(使用Intent.ACTION_PICK)。然后我得到了重新presents在数据库中的特定联系人的URI。

不幸的是,我不知道是什么,下一步就是。看起来这应该是世界上最简单的做法,但显然不是。我已经通过在Android开发者网站上的文档阅读,我已经经历了多个Android的书看。没有快乐。

我想获得具体信息,是:

  1. 联系人的姓名(第一次和最后分开,如果可能的话)

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

  3. 联系人的手机号码

我想,这应该是可以通过使用ContentResolver的查询,但我不知道如何使用URI做到这一点从意图返回。大部分的文档假设你有联系的ID,而不是联系人的URI。另外,我不知道什么样的领域,我可以放入投影查询,假设这是连正确的方式做我想做的。

下面是我的出发code:

  //在按钮的onClick事件处理程序:
意向意图=新的意图(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(意向,PICK_CONTACT);

//在onActivityResult:
如果(结果code == RESULT_OK){
    如果(要求code == PICK_CONTACT){
        contactURI = data.getData();
        // 怎么办?
    }
}
 

解决方案

事实证明,有一个更好的方式来做到这一点。

正如我所提到的,ContactsContract.Contacts.Entity类是不可用,直到API 11.然而,<一个href="http://developer.android.com/reference/android/provider/ContactsContract.Data.html">ContactsContract.Data类是早在5 API提供的方法,你可以使用这个类在很大程度上您所使用的实体类的方法相同。

我已经更新了我的code。它是非常相似的code代表实体类,并工作在很大程度上以相同的方式。不过,我有我的手机上运行姜饼测试它,它工作得很好。

一个变化,我不得不做的是:似乎没有成为一个方式来获得 ContactsContract.Data.RAW_CONTACT_ID 从最初的查询,而且ID是的没有的一样,你从如获得ID ContactsContract.Contacts._ID 。相反,我询问关于 ContactsContract.Contacts.DISPLAY_NAME 常量,它是跨pretty的一致多少每个ContactsContract类。

这里的工作code:

 光标指针; // Cursor对象
        字符串哑剧; // MIME类型
        INT dataIdx; DATA1列//指数
        INT mimeIdx; MIMETYPE列//指数
        INT nameIdx; DISPLAY_NAME列//指数

        //获取名称
        光标= getContentResolver()。查询(PARAMS [0],
                新的String [] {} ContactsContract.Contacts.DISPLAY_NAME,
                NULL,NULL,NULL);
        如果(cursor.moveToFirst()){
            nameIdx = cursor.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME);
            名称= cursor.getString(nameIdx);

            //设置投影
            的String []投影= {
                    ContactsContract.Data.DISPLAY_NAME,
                    ContactsContract.Contacts.Data.DATA1,
                    ContactsContract.Contacts.Data.MIMETYPE};

            //查询ContactsContract.Data
            光标= getContentResolver()查询(
                    ContactsContract.Data.CONTENT_URI,投影,
                    ContactsContract.Data.DISPLAY_NAME +=?,
                    新的String [] {名}
                    空值);

            如果(cursor.moveToFirst()){
                //获取MIME类型和数据的索引
                mimeIdx = cursor.getColumnIndex(
                        ContactsContract.Contacts.Data.MIMETYPE);
                dataIdx = cursor.getColumnIndex(
                        ContactsContract.Contacts.Data.DATA1);

                //匹配数据的MIME类型,存储在变量
                做 {
                    默= cursor.getString(mimeIdx);
                    如果(ContactsContract.CommonDataKinds.Email
                            .CONTENT_ITEM_TYPE.equalsIgnoreCase(MIME)){
                        电子邮件= cursor.getString(da​​taIdx);
                    }
                    如果(ContactsContract.CommonDataKinds.Phone
                            .CONTENT_ITEM_TYPE.equalsIgnoreCase(MIME)){
                        手机= cursor.getString(da​​taIdx);
                        手机= PhoneNumberUtils.formatNumber(电话);
                    }
                }而(cursor.moveToNext());
            }
        }
 

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.)

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.

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. The contact's name (first and last separately if possible)

  2. The contact's (primary) email address

  3. The contact's cell phone number

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.

Here is my starting code:

// 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.

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.

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.

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.

Here's the working code:

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

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

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