【问题标题】:ContactsContract.Data.IS_READ_ONLY returns negative value -1ContactsContract.Data.IS_READ_ONLY 返回负值 -1
【发布时间】:2018-08-28 06:43:37
【问题描述】:

IS_READ_ONLY

flag:默认为“0”,如果行不能被修改或删除,则为“1” 除了同步适配器。请参阅 CALLER_IS_SYNCADAPTER。类型:整数 常量值:“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);

【问题讨论】:

  • 您查询的是正确的端点吗?尝试查询ContactsContract.Data.CONTENT_URI
  • @MichaelDodd 谢谢。尝试了您的建议,但输出仍然相同。

标签: android android-contentresolver contactscontract android-cursorloader


【解决方案1】:

您的代码中有两个错误:

  1. 如 cmets 中所述,您查询的是错误的表,要访问 Data.* 列,您需要查询 Data.CONTENT_URI
  2. Cursor.getColumnIndex 将返回投影中指定列的索引,而不是存储在该字段中的值,-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);
}

【讨论】:

  • 执行您的代码时出现以下错误。 java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.IllegalArgumentException: Invalid column is_read_only
  • start activity ?你从哪里运行这个?
  • 我从类中的 onCreate 方法运行此代码,扩展 AppCompatActivity
  • 听起来您从错误的表中查询,请确保您正在导入正确的 Data 表 - 即:android.provider.ContactsContract.Data
【解决方案2】:

我已经使用下面的方法来获取只读帐户,然后我已经从中提取了联系人。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 2016-10-07
    • 2012-02-23
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多