ContactsContract.Data.IS_READ_ONLY返回负值-1

时间:2018-03-19 16:08:32

标签: android android-contentresolver contactscontract android-cursorloader

  

IS_READ_ONLY

     

标志:默认为“0”,如果无法修改或删除行,则为“1”   除了同步适配器。请参阅CALLER_IS_SYNCADAPTER。类型:INTEGER   常数值:“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));
            }
        }

输出

-1
-1
-1

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

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

2 个答案:

答案 0 :(得分:0)

您的代码中有两个错误:

  1. 正如评论中所述,您在错误的表格上查询,要访问Data.*
  2. 要查询的Data.CONTENT_URI
  3. Cursor.getColumnIndex将返回投影中指定列的索引,而不是该字段中存储的值,-1表示投影中不存在此列。
  4. 试试这个:

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

答案 1 :(得分:0)

我使用以下方法获取只读帐户,然后我从中删除了联系人。

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