查询 Android 联系人以获取 ACCOUNT_TYPE 和 ACCOUNT_NAME

发布于 2024-11-29 13:33:36 字数 380 浏览 5 评论 0原文

我可以获得联系人列表及其基本信息,例如:姓名。通过使用 ContactsContract.Contacts.CONTENT_URI 获取联系人列表以及不同信息类型的其他特定 URI,以实现电话、电子邮件、即时消息、笔记、组织的备份。

为了完全恢复所有信息,我需要另外两个字段:

ContactsContract.RawContacts.ACCOUNT_TYPE

ContactsContract.RawContacts.ACCOUNT_NAME

任何人都可以指导我如何在从 ContactsContract.Contacts.CONTENT_URI 知道联系人 ID 的情况下获取此信息吗?

谢谢

I am able to obtain a list of contacts and their basic information like: name. phones, emails, ims, notes, organizations for backup purposes by using ContactsContract.Contacts.CONTENT_URI for a list of Contacts and other specific URIs for different information type.

I need, in order to fully restore all the information two more fields:

ContactsContract.RawContacts.ACCOUNT_TYPE

ContactsContract.RawContacts.ACCOUNT_NAME

Can anyone guide me how to obtain this info, knowing the Contact Id from ContactsContract.Contacts.CONTENT_URI ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-12-06 13:33:36
public ContactAccount getContactAccount(Long id,ContentResolver contentResolver) {

        ContactAccount account = null;

        Cursor cursor = null;
        try {

             cursor = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI,
                     new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
                     ContactsContract.RawContacts.CONTACT_ID +"=?",
                     new String[]{String.valueOf(id)},
                     null);

            if (cursor != null && cursor.getCount() >0)
            {
                cursor.moveToFirst();
                account = new ContactAccount();
                account.setAccountName(cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME)));
                account.setAccountType(cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)));
                cursor.close();
            }
        } catch (Exception e) {
            Utils.log(this.getClass().getName(), e.getMessage()); 
        } finally{
          cursor.close();
        }

        return(account);
    }
public ContactAccount getContactAccount(Long id,ContentResolver contentResolver) {

        ContactAccount account = null;

        Cursor cursor = null;
        try {

             cursor = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI,
                     new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
                     ContactsContract.RawContacts.CONTACT_ID +"=?",
                     new String[]{String.valueOf(id)},
                     null);

            if (cursor != null && cursor.getCount() >0)
            {
                cursor.moveToFirst();
                account = new ContactAccount();
                account.setAccountName(cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME)));
                account.setAccountType(cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)));
                cursor.close();
            }
        } catch (Exception e) {
            Utils.log(this.getClass().getName(), e.getMessage()); 
        } finally{
          cursor.close();
        }

        return(account);
    }
仅此而已 2024-12-06 13:33:36

如果您正在使用 contactID 列查找帐户信息,以上答案是完美的。但是,信息通常是使用 rawContactID 存储的。因此,如果您想访问原始联系人 ID 的帐户信息,则可以使用下面的方法。
主要区别在于我使用的是 rawContacts 表中的 _ID 列。这映射到您将在其他表中看到的 rawContactID

public int   updateAccountInfoForContactData(String rawContactID) {
    int    accountPos    = 0;

    Cursor cursor        = null;
    String accountName   = null;
    String accountType   = null;
    Uri rawContactUri      = ContactsContract.RawContacts.CONTENT_URI;
    String[] syncColumns   = new String[] {
                                            ContactsContract.RawContacts.ACCOUNT_NAME,
                                            ContactsContract.RawContacts.ACCOUNT_TYPE,
                                    };
    String   whereClause   = ContactsContract.RawContacts._ID +"=?";
    String[] whereParams   = new String[]{String.valueOf(rawContactID)};
    //Uri rawContactUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, longContactID);
    try {
        cursor = mContext.getContentResolver().query(
                rawContactUri,
                syncColumns,
                whereClause,
                whereParams,
                null);

        if (cursor != null && cursor.getCount() >0)
        {
            cursor.moveToFirst();
            if(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME) >= 0) {
                accountName = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
            }
            if(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE) >= 0) {
                accountType = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
            }
            cursor.close();
            cursor = null;

        }
    } catch (Exception e) {
        Log.d(TAG, "getting account info failed");
    } finally{
        if(cursor != null) {
            cursor.close();
        }
        cursor = null;
    }

    return(accountPos);
}

The above answer is perfect if you are looking for account information using the contactID column. But, often information is stored using rawContactID. So, if you want to access account information for a raw-contact-id then you can use this method below.
The key difference is that I am using the _ID column from the rawContacts table. This maps to the rawContactID that you will see in other tables

public int   updateAccountInfoForContactData(String rawContactID) {
    int    accountPos    = 0;

    Cursor cursor        = null;
    String accountName   = null;
    String accountType   = null;
    Uri rawContactUri      = ContactsContract.RawContacts.CONTENT_URI;
    String[] syncColumns   = new String[] {
                                            ContactsContract.RawContacts.ACCOUNT_NAME,
                                            ContactsContract.RawContacts.ACCOUNT_TYPE,
                                    };
    String   whereClause   = ContactsContract.RawContacts._ID +"=?";
    String[] whereParams   = new String[]{String.valueOf(rawContactID)};
    //Uri rawContactUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, longContactID);
    try {
        cursor = mContext.getContentResolver().query(
                rawContactUri,
                syncColumns,
                whereClause,
                whereParams,
                null);

        if (cursor != null && cursor.getCount() >0)
        {
            cursor.moveToFirst();
            if(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME) >= 0) {
                accountName = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
            }
            if(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE) >= 0) {
                accountType = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
            }
            cursor.close();
            cursor = null;

        }
    } catch (Exception e) {
        Log.d(TAG, "getting account info failed");
    } finally{
        if(cursor != null) {
            cursor.close();
        }
        cursor = null;
    }

    return(accountPos);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文