【问题标题】:How to get read only account names from contact id in Contacts in Android如何从 Android 联系人中的联系人 ID 获取只读帐户名称
【发布时间】:2018-01-25 22:37:09
【问题描述】:

我正在将列表中的帐户名称传递给此方法。现在我想知道哪些帐户名称在联系人表中是只读的,所以我只迭代游标一次以从原始游标中获取联系人 ID。获得contact_id后,我正在使用手机光标检查给定的id是否为只读,但我无法做到。请往下看

 private void displayAllContactsByType(String accountName) {

    Cursor rawCursor,phones = null;

    rawCursor = cResolver.query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts.CONTACT_ID},
            ContactsContract.RawContacts.ACCOUNT_NAME + "= ?",
            new String[]{accountName},
            null);


    int contactIdColumn = rawCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
    int rawCursorCount = rawCursor.getCount();


    Utils.Log("Account Name",  accountName);

    Utils.Log("Raw Size", " " + rawCursorCount);
    rawCursor.moveToFirst();
    Long contactId = rawCursor.getLong(contactIdColumn);


    phones = cResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
           null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND "+ContactsContract.RawContacts.ACCOUNT_NAME + "= ?",
            new String[]{String.valueOf(contactId),accountName},
            null);

    phones.moveToFirst();




   String isReadOnly= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_READ_ONLY));
    Utils.Log("Raw Size", isReadOnly);


}

【问题讨论】:

    标签: android android-contacts android-contentresolver rawcontacts


    【解决方案1】:

    您不必检查帐户的联系人来检查,您可以简单地遍历设备上的 SyncAdapter,并检查它们的属性:

    final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
    for (SyncAdapterType sync : syncs) {
        Log.d(TAG, "found SyncAdapter: " + sync.accountType);
        if (ContactsContract.AUTHORITY.equals(sync.authority)) {
            Log.d(TAG, "SyncAdapter supports contacts: " + sync.accountType);
            boolean readOnly = !sync.supportsUploading();
            Log.d(TAG, "SyncAdapter read-only mode: " + readOnly);
            if (readOnly) {
                // we'll now get a list of all accounts under that accountType:
                Account[] accounts = AccountManager.get(this).getAccountsByType(sync.accountType);
                for (Account account : accounts) {
                   Log.d(TAG, account.type + " / " + account.name);
                }
            }
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 非常感谢。
    • 我还想只获得那些只有手机联系人的带有同步适配器的帐户。
    • having phone contacts only 是什么意思?您的意思是称为“仅电话(未同步)”的特殊联系人帐户,如果您这样做,它没有 SyncAdapter,因为它是一个未同步的帐户
    • 实际上我正在创建联系人复制器应用程序,我需要在其中同步所有联系人的帐户名称,但上面的代码还提供了未同步的帐户名称,如linkedin、messanger(FB)、Office 等我猜是联系方式。我只需要 google duo、whats app、freecharge、mobikwik 等帐户,我的所有联系人都已同步。
    • linkedin、office等都能够同步通讯录,比如进入linkedin应用设置,可以开启通讯录同步,这种情况下您将开始在手机中看到linkedin 联系人。如果您想实际验证是否有来自该帐户的联系人,您可以在 RawContacts 表上使用ACCOUNT_NAME + "='" accountName + "'" 查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多