如何使用 ContentResolver 获取帐户类型?
How can i get account type using ContentResolver?
如何使用 ContentResolver 检索帐户类型(whatsapp、viber 等)?
这是我的代码
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Log.d("log : ","name : " + name + ", ID : " + id);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phone1 = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.d("log : ","phone" + phone + " type : "+phone1);
}
pCur.close();
Android 的通讯录中的数据分为 3 个主要 table:
Contacts
- 每个条目代表一个联系人,并将一个或多个 RawContacts
组合在一起
RawContacts
- 每个条目代表有关联系人的数据,这些数据由某些 SyncAdapter
(例如 Whatsapp、Google、Facebook、Viber)同步,这将多个数据条目分组
Data
- 有关联系人、电子邮件、电话等的实际数据。每行是属于单个 RawContact
的单个数据
您的代码存在的问题是您遍历了所有 Contacts
,然后查询每个联系人的电话号码。您查询的不是 RawContact
table,其中包含您想要的 ACCOUNT_NAME
和 ACCOUNT_TYPE
。另外,为了获取设备上的所有电话而进行数百次查询是非常低效的,您可以改为使用单个查询来完成。
// First create a mapping from RAW_CONTACT_ID to ACCOUNT_TYPE (e.g. Whatsapp)
Map<Long, String> rawContacts = new HashMap<Long, String>();
String[] projection1 = {RawContacts._ID, RawContacts.ACCOUNT_TYPE};
Cursor cur1 = cr.query(RawContacts.CONTENT_URI, projection1, null, null, null);
while (cur1 != null && cur1.moveToNext()) {
Long id = cur1.getLong(0);
String account = cur1.getString(1);
rawContacts.put(id, account);
}
// Now query for all the PHONES on the device (no need to query the Contacts table at all)
String[] projection2 = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.RAW_CONTACT_ID };
Cursor cur2 = cr.query(Phone.CONTENT_URI, projection2, null, null, null);
while (cur2 != null && cur2.moveToNext()) {
long id = cur2.getLong(0);
String name = cur2.getString(1);
String number = cur2.getString(2); // the actual info, e.g. +1-212-555-1234
int type = cur2.getInt(3); // a numeric value representing type: e.g. home / office / personal
long raw = cur2.getLong(4);
Log.d(TAG, "contact: " + id + ", name: " + name + ", number: " + number + ", type: " + type + ", raw-id: " + raw + ", account-type: " + rawContacts.get(raw));
}
P.S.
您可以创建第二个 HashMap
并使用 CONTACT_ID
作为键,将每个联系人的有关单个联系人的所有信息分组。
如何使用 ContentResolver 检索帐户类型(whatsapp、viber 等)?
这是我的代码
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Log.d("log : ","name : " + name + ", ID : " + id);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phone1 = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.d("log : ","phone" + phone + " type : "+phone1);
}
pCur.close();
Android 的通讯录中的数据分为 3 个主要 table:
Contacts
- 每个条目代表一个联系人,并将一个或多个RawContacts
组合在一起
RawContacts
- 每个条目代表有关联系人的数据,这些数据由某些SyncAdapter
(例如 Whatsapp、Google、Facebook、Viber)同步,这将多个数据条目分组Data
- 有关联系人、电子邮件、电话等的实际数据。每行是属于单个RawContact
的单个数据
您的代码存在的问题是您遍历了所有 Contacts
,然后查询每个联系人的电话号码。您查询的不是 RawContact
table,其中包含您想要的 ACCOUNT_NAME
和 ACCOUNT_TYPE
。另外,为了获取设备上的所有电话而进行数百次查询是非常低效的,您可以改为使用单个查询来完成。
// First create a mapping from RAW_CONTACT_ID to ACCOUNT_TYPE (e.g. Whatsapp)
Map<Long, String> rawContacts = new HashMap<Long, String>();
String[] projection1 = {RawContacts._ID, RawContacts.ACCOUNT_TYPE};
Cursor cur1 = cr.query(RawContacts.CONTENT_URI, projection1, null, null, null);
while (cur1 != null && cur1.moveToNext()) {
Long id = cur1.getLong(0);
String account = cur1.getString(1);
rawContacts.put(id, account);
}
// Now query for all the PHONES on the device (no need to query the Contacts table at all)
String[] projection2 = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, Phone.RAW_CONTACT_ID };
Cursor cur2 = cr.query(Phone.CONTENT_URI, projection2, null, null, null);
while (cur2 != null && cur2.moveToNext()) {
long id = cur2.getLong(0);
String name = cur2.getString(1);
String number = cur2.getString(2); // the actual info, e.g. +1-212-555-1234
int type = cur2.getInt(3); // a numeric value representing type: e.g. home / office / personal
long raw = cur2.getLong(4);
Log.d(TAG, "contact: " + id + ", name: " + name + ", number: " + number + ", type: " + type + ", raw-id: " + raw + ", account-type: " + rawContacts.get(raw));
}
P.S.
您可以创建第二个 HashMap
并使用 CONTACT_ID
作为键,将每个联系人的有关单个联系人的所有信息分组。