如何从android contacts内容提供商处获取不重复的电话号码
我正在尝试获取所有有手机号码的联系人。 我能够获取联系人。但问题是它会给我重复的联系人。相同的电话号码会出现两次 我使用以下代码获取联系人如何从android contacts内容提供商处获取不重复的电话号码,android,android-contacts,Android,Android Contacts,我正在尝试获取所有有手机号码的联系人。 我能够获取联系人。但问题是它会给我重复的联系人。相同的电话号码会出现两次 我使用以下代码获取联系人 public static List<RawContact> getAllContacts(Context context,Account account){ Log.d(TAG, "*** Looking for local contacts with mobile number!!"); String pho
public static List<RawContact> getAllContacts(Context context,Account account){
Log.d(TAG, "*** Looking for local contacts with mobile number!!");
String phoneNumber = null;
String email = null;
int numberOfContacts = 0;
List<RawContact> newContacts = new ArrayList<RawContact>();
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
List<RawContact> localNewContacts = new ArrayList<RawContact>();
final ContentResolver contentResolver = context.getContentResolver();
final Cursor cursor = contentResolver.query(CONTENT_URI,
null,
null,
null,
null);
if(cursor.getCount()>0){
numberOfContacts = 0;
Log.d(TAG, "*** Looking for local contacts "+cursor.getCount());
while(cursor.moveToNext()){
String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
final long rawContactId = cursor.getLong(DirtyQuery.COLUMN_RAW_CONTACT_ID);
if (hasPhoneNumber > 0) {
//This is to read multiple phone numbers associated with the same contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, ContactsContract.RawContacts.ACCOUNT_TYPE + " <> 'google' "+" AND "+ Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);
while (phoneCursor.moveToNext()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
Log.d(TAG,"Phone number is: "+phoneNumber);
RawContact rawContact = getRawContact(context, rawContactId);
Log.d(TAG, "Contact Name: " + rawContact.getBestName());
localNewContacts.add(rawContact);
}phoneCursor.close();
}
numberOfContacts++;
Log.d(TAG, "numberOfContacts updated: "+numberOfContacts);
}
}
return localNewContacts;
}
公共静态列表getAllContacts(上下文、帐户){
Log.d(标记“***正在使用手机号码查找本地联系人!!”;
字符串phoneNumber=null;
字符串email=null;
int numberOfContacts=0;
List newContacts=new ArrayList();
Uri CONTENT\u Uri=Contacts contract.Contacts.CONTENT\u Uri;
String _ID=Contacts contact.Contacts.\u ID;
String DISPLAY\u NAME=Contacts contract.Contacts.DISPLAY\u NAME;
字符串HAS\u PHONE\u NUMBER=Contacts contract.Contacts.HAS\u PHONE\u NUMBER;
Uri PhoneCONTENT\u Uri=ContactsContract.CommonDataTypes.Phone.CONTENT\u Uri;
字符串Phone\u CONTACT\u ID=contacts contract.commondatatypes.Phone.CONTACT\u ID;
字符串编号=contacts contract.commonDataTypes.Phone.NUMBER;
List localNewContacts=newarraylist();
final ContentResolver ContentResolver=context.getContentResolver();
最终游标=contentResolver.query(CONTENT\u URI,
无效的
无效的
无效的
无效);
if(cursor.getCount()>0){
接触人数=0;
Log.d(标记“***查找本地联系人”+cursor.getCount());
while(cursor.moveToNext()){
String contact_id=cursor.getString(cursor.getColumnIndex(_id));
字符串名称=cursor.getString(cursor.getColumnIndex(DISPLAY_name));
int hasPhoneNumber=Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER));
final long rawContactId=cursor.getLong(DirtyQuery.COLUMN\u RAW\u CONTACT\u ID);
如果(hasPhoneNumber>0){
//这是为了读取与同一联系人关联的多个电话号码
Cursor phoneCursor=contentResolver.query(PhoneCONTENT\u URI,null,ContactsContract.RawContacts.ACCOUNT\u TYPE+“'google'”+”和“+Phone\u CONTACT\u ID+”=?”,新字符串[]{CONTACT\u ID},null);
while(phoneCursor.moveToNext()){
phoneNumber=phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
Log.d(标签,“电话号码为:”+电话号码);
RawContact RawContact=getRawContact(上下文,rawContactId);
Log.d(标记“联系人名称:”+rawContact.getBestName());
localNewContacts.add(rawscontact);
}phoneCursor.close();
}
numberOfContacts++;
Log.d(标签“numberOfContacts更新:”+numberOfContacts);
}
}
返回localNewContacts;
}
如何解决此问题您获得的是重复的联系人,因为您实际上是在阅读
原始联系人
,而不是联系人。多个RawContact
s可以并且将包含由单个联系人所代表的单个人的信息
您需要在Phones
表上进行一次查询,并使用HashMap将数据从CONTACT\u ID
组织到CONTACT info,这样您就不会得到重复的数据
联系人数据库分为三个主要表格:
联系人
-每个条目代表一个联系人,并将一个或多个联系人分组
RawContacts
-每个条目表示由某些SyncAdapter
(例如Whatsapp、Google、Facebook、Viber)同步的联系人的数据,这将多个数据条目分组
数据
-关于联系人、电子邮件、电话等的实际数据。每一行都是属于单个联系人的一段数据
您试图在联系人
表上进行查询,并从数据
表中投影字段,您不能这样做
您可以使用如下代码,只需将HashMap
转换为使用您的ContactModel
对象即可:
Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();
String[] projection = { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3 };
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "')";
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
String mime = cur.getString(2); // type of data (e.g. "phone")
String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
int type = cur.getInt(4); // a numeric value representing type: e.g. home / office / personal
String label = cur.getString(5); // a custom label in case type is "TYPE_CUSTOM"
String labelStr = Phone.getTypeLabel(getResources(), type, label);
Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data + " (" + labelStr + ")");
// add info to existing list if this contact-id was already found, or create a new list in case it's new
List<String> infos;
if (contacts.containsKey(id)) {
infos = contacts.get(id);
} else {
infos = new ArrayList<String>();
infos.add("name = " + name);
contacts.put(id, infos);
}
infos.add(kind + " = " + data + " (" + labelStr + ")");
}
Map contacts=newhashmap();
String[]projection={Data.CONTACT_ID,Data.DISPLAY_NAME,Data.MIMETYPE,Data.DATA1,Data.DATA2,Data.DATA3};
字符串选择=Data.MIMETYPE+“(““+Phone.CONTENT_ITEM_TYPE+”)”中的”;
游标cur=cr.query(Data.CONTENT\u URI,投影,选择,null,null);
while(cur!=null&&cur.moveToNext(){
long id=cur.getLong(0);
字符串名称=cur.getString(1);
字符串mime=cur.getString(2);//数据类型(例如“电话”)
String data=cur.getString(3);//实际信息,例如+1-212-555-1234
int type=cur.getInt(4);//表示类型的数值:例如家庭/办公室/个人
String label=cur.getString(5);//大小写类型为“type_custom”时的自定义标签
String labelStr=Phone.getTypeLabel(getResources(),type,label);
Log.d(标记,“got”+id+“,“+name+”,“+kind+”-“+data+”(“+labelStr+”));
//如果已经找到此联系人id,请将信息添加到现有列表中,如果是新的,请创建一个新列表
列出信息;
if(联系人:contacts.containsKey(id)){
infos=contacts.get(id);
}否则{
infos=newarraylist();
infos.add(“name=“+name”);
联系人.put(id,infos);
}
信息添加(种类+“=”+数据+”(“+labelStr+”);
}
t您也可以尝试以下链接:got 411,位于萨拉瓦南,kind-+44 7438 306573(手机)04-18 12:11:19.142 9560-9939/com.example.android.samplesync D/ContactManager:Result executed!!04-18 12:11:19.142 9560-9939/com.example.android.samplesync D/ContactManager:got 411,在萨拉瓦南,kind-+447438306573(手机)04-18 12:11:19.142 9560-9939/com.example.android.samplesync D/ContactManager:Result executed!!我得到了这样的结果是的