ContactsContract.Data.IS_READ_ONLY returns负值-1

ContactsContract.Data.IS_READ_ONLY returns negative value -1

IS_READ_ONLY

flag: "0" by default, "1" if the row cannot be modified or deleted except by a sync adapter. See CALLER_IS_SYNCADAPTER. Type: INTEGER Constant Value: "is_read_only"

当我在我的代码中应用以上内容时,我得到 -1 作为所有联系人的输出。我正在使用 IS_READ_ONLY 来识别在 WhatsApp、PayTM、Duo 等中同步的只读联系人

Cursor curContacts = cr.query(ContactsContract.Contacts.CONTENT_URI, null,  null, null, null);
        if (curContacts != null) {
            while (curContacts.moveToNext()) {
                int contactsReadOnly = curContacts.getColumnIndex(ContactsContract.Data.IS_READ_ONLY);
                Log.d(Config.TAG, String.valueOf(contactsReadOnly));
            }
        }

OutPut

-1
-1
-1

也尝试了以下行而不是 Data.IS_READ_ONLY,但输出是相同的。

int contactsReadOnly = curContacts.getColumnIndex(ContactsContract.RawContacts.RAW_CONTACT_IS_READ_ONLY);

您的代码中有两个错误:

  1. 如评论中所述,您正在查询错误的 table,要访问 Data.* 列,您需要查询 Data.CONTENT_URI
  2. Cursor.getColumnIndex 将 return 指定列在您的投影中的索引,而不是存储在该字段中的值,-1 表示该列在您的投影中不存在。

试试这个:

String[] projection = new String[] { Data.IS_READ_ONLY };
Cursor curData = cr.query(ContactsContract.Data.CONTENT_URI, projection, null, null, null);
while (curData != null && curData.moveToNext()) {
    int dataReadOnly = curData.getInt(0); // 0 because it is the first field in the projection
    Log.d(Config.TAG, "data is: " + dataReadOnly);
}

我使用以下方法获取了 read-only 个帐户,然后从他们那里提取了联系人。

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