如何从Android中的联系人中的联系人ID获取只读帐户名称
问题描述:
我将帐户名称从列表传递给此方法。现在我想知道哪些这些帐户名称只在联系人表中读取,所以我只迭代光标一次从原始光标获取联系人ID。获取contact_id后,我使用电话游标来检查给定的ID是否只读,但我无法做到。请有以下如何从Android中的联系人中的联系人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);
}
答
你不必去了一个帐户的联系人来检查看看,你可以简单地在设备上的SyncAdapters迭代,并检查它们的属性:
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);
}
}
}
}
希望这可以帮助。
Thankyou非常多。 –
我也想只与具有电话联系人的同步适配器一起使用这些帐户。 –
“只有手机通讯录”是什么意思?你的意思是特殊的联系人帐户名为“仅电话(未同步)”,如果你这样做,它没有SyncAdapter,因为它是一个未初始帐户 – marmor