IS_READ_ONLY
标志:默认为"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);
你的代码中有两个错误:
- 如注释中所述,您查询了错误的表,要访问
Data.*
列,您需要通过Data.CONTENT_URI
进行查询 -
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);
}
我使用以下方法来获取只读帐户,然后从中提取联系人。
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);
}
}
}
}