Android 如何使用ContentResolver获取帐户类型?
如何使用ContentResolver检索帐户类型(whatsapp、viber等?Android 如何使用ContentResolver获取帐户类型?,android,android-contacts,android-contentresolver,Android,Android Contacts,Android 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()) {
这是我的密码
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个主表:
联系人
-每个条目代表一个联系人,并将一个或多个联系人分组
RawContacts
-每个条目表示由某些SyncAdapter
(例如Whatsapp、Google、Facebook、Viber)同步的联系人的数据,这将多个数据条目分组数据
-关于联系人、电子邮件、电话等的实际数据。每一行都是属于单个联系人的一段数据
联系人
,然后查询每个联系人的电话。您没有查询RawContact
表,该表包含所需的ACCOUNT\u名称
和ACCOUNT\u类型
。另外,仅仅为了在设备上获取所有手机而进行数百次查询是非常低效的,你可以用一次查询来代替
// 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));
}
//首先创建从原始联系人ID到帐户类型的映射(例如Whatsapp)
Map rawscontacts=newhashmap();
字符串[]projection1={RawContacts.\u ID,RawContacts.ACCOUNT\u TYPE};
Cursor cur1=cr.query(rawscontacts.CONTENT_URI,projection1,null,null);
while(cur1!=null&&cur1.moveToNext(){
Long id=cur1.getLong(0);
String account=cur1.getString(1);
rawscontacts.put(id,account);
}
//现在查询设备上的所有电话(根本不需要查询联系人表)
String[]projection2={Phone.CONTACT\u ID,Phone.DISPLAY\u NAME,Phone.NUMBER,Phone.TYPE,Phone.RAW\u CONTACT\u ID};
游标cur2=cr.query(Phone.CONTENT\u URI,projection2,null,null,null);
while(cur2!=null&&cur2.moveToNext(){
long id=cur2.getLong(0);
字符串名称=cur2.getString(1);
String number=cur2.getString(2);//实际信息,例如+1-212-555-1234
int type=cur2.getInt(3);//表示类型的数值:例如家庭/办公室/个人
long raw=cur2.getLong(4);
Log.d(标签,“联系人:+id+”,姓名:+name+”,号码:+number+,类型:+type+,原始id:+raw+,帐户类型:+rawContacts.get(原始));
}
p.S.
您可以创建第二个
HashMap
,其中CONTACT_ID
作为键,对每个联系人的单个联系人的所有信息进行分组。什么是phone1
给您的?phone1给我电话号码类型(工作、家庭等),但我需要帐户类型(whatsapp、viber等)