java - Obtain matching contact email and phone from android phone -
i have managed extract contact details phone using contactcontract example found, noticed of people on phone has unique id key associated emails , phone numbers separately. example, alan's contact detail split following when extract out database though same person:
key name email phone 20121 alan alan@gmail.com null 20133 alan null 04xxxxxxxx
so how phone manage association these different keys in contact (i assume there must separate table it)? there way obtain association? because can not try match name people can have same name, have keep them separated how stored on phone contact.
(or messed situation due apps able save contact related details same database on phone?)
my code looks following (i forgot code from, getdetailedcontactlist function returning list of contact of above problem):
public static string contact_id_uri = contactscontract.contacts._id; public static string data_contact_id_uri = contactscontract.data.contact_id; public static string mimetype_uri = contactscontract.data.mimetype; public static string email_uri = contactscontract.commondatakinds.email.data; public static string phone_uri = contactscontract.commondatakinds.phone.data; public static string name_uri = (build.version.sdk_int >= build.version_codes.honeycomb) ? contactscontract.data.display_name_primary : contactscontract.data.display_name; public static string picture_uri = (build.version.sdk_int >= build.version_codes.honeycomb) ? contactscontract.contacts.photo_thumbnail_uri : contactscontract.contacts.photo_id; public static string mail_type = contactscontract.commondatakinds.email.content_item_type; public static string phone_type = contactscontract.commondatakinds.phone.content_item_type; public cursor getcontactcursor(string stringquery, string sortorder) { log.i(tag, "+++++++++++++++++++++++++++++++++++++++++++++++++++"); log.e(tag, "contactcursor search has started..."); long t0 = system.currenttimemillis(); uri content_uri; if (stringquery == null) content_uri = contactscontract.contacts.content_uri; else content_uri = uri.withappendedpath(contactscontract.contacts.content_filter_uri, uri.encode(stringquery)); string[] projection = new string[]{ contact_id_uri, name_uri, picture_uri }; string selection = name_uri + " not ?"; string[] selection_args = new string[]{"%" + "@" + "%"}; cursor cursor = getcontentresolver().query(content_uri, projection, selection, selection_args, sortorder); long t1 = system.currenttimemillis(); log.e(tag, "contactcursor finished in " + (t1 - t0) / 1000 + " secs"); log.e(tag, "contactcursor found " + cursor.getcount() + " contacts"); log.i(tag, "+++++++++++++++++++++++++++++++++++++++++++++++++++"); return cursor; } public cursor getcontactdetailscursor() { log.i(tag, "+++++++++++++++++++++++++++++++++++++++++++++++++++"); log.e(tag, "contactdetailscursor search has started..."); long t0 = system.currenttimemillis(); string[] projection = new string[]{ data_contact_id_uri, mimetype_uri, email_uri, phone_uri }; string selection = name_uri + " not ?" + " , " + "(" + mimetype_uri + "=? " + " or " + mimetype_uri + "=? " + ")"; string[] selection_args = new string[]{"%" + "@" + "%", contactscontract.commondatakinds.email.content_item_type, contactscontract.commondatakinds.phone.content_item_type}; cursor cursor = getcontentresolver().query( contactscontract.data.content_uri, projection, selection, selection_args, null); long t1 = system.currenttimemillis(); log.e(tag, "contactdetailscursor finished in " + (t1 - t0) / 1000 + " secs"); log.e(tag, "contactdetailscursor found " + cursor.getcount() + " contacts"); log.i(tag, "+++++++++++++++++++++++++++++++++++++++++++++++++++"); return cursor; } public list<contactviewmodel> getdetailedcontactlist(string querystring) { /** * first fetch contacts name , picture uri in alphabetical order * display purpose , store these data in hashmap. */ cursor contactcursor = getcontactcursor(querystring, name_uri); if(contactcursor.getcount() == 0){ contactcursor.close(); return new arraylist<>(); } list<integer> contactids = new arraylist<>(); if (contactcursor.movetofirst()) { { contactids.add(contactcursor.getint(contactcursor.getcolumnindex(contact_id_uri))); } while (contactcursor.movetonext()); } hashmap<integer, string> namemap = new hashmap<>(); hashmap<integer, string> picturemap = new hashmap<>(); int ididx = contactcursor.getcolumnindex(contact_id_uri); int nameidx = contactcursor.getcolumnindex(name_uri); int pictureidx = contactcursor.getcolumnindex(picture_uri); if (contactcursor.movetofirst()) { { namemap.put(contactcursor.getint(ididx), contactcursor.getstring(nameidx)); picturemap.put(contactcursor.getint(ididx), contactcursor.getstring(pictureidx)); } while (contactcursor.movetonext()); } /** * remaining contact information. here email , phone */ cursor detailscursor = getcontactdetailscursor(); hashmap<integer, string> emailmap = new hashmap<>(); hashmap<integer, string> phonemap = new hashmap<>(); ididx = detailscursor.getcolumnindex(data_contact_id_uri); int mimeidx = detailscursor.getcolumnindex(mimetype_uri); int mailidx = detailscursor.getcolumnindex(email_uri); int phoneidx = detailscursor.getcolumnindex(phone_uri); string mailstring; string phonestring; if (detailscursor.movetofirst()) { { /** * forget details not correlated contact list */ if (!contactids.contains(detailscursor.getint(ididx))) { continue; } if(detailscursor.getstring(mimeidx).equals(mail_type)){ mailstring = detailscursor.getstring(mailidx); /** * remove double contact having same email address */ if(!emailmap.containsvalue(mailstring.tolowercase())) emailmap.put(detailscursor.getint(ididx), mailstring.tolowercase()); } else { phonestring = detailscursor.getstring(phoneidx); phonemap.put(detailscursor.getint(ididx), phonestring); } } while (detailscursor.movetonext()); } contactcursor.close(); detailscursor.close(); /** * contact list build */ list<contactviewmodel> contacts = new arraylist<>(); set<integer> emailskeyset = emailmap.keyset(); set<integer> phonekeyset = phonemap.keyset(); (integer key : contactids) { if( (!emailskeyset.contains(key) && !phonekeyset.contains(key)) || (emailmap.get(key) == null && phonemap.get(key) == null) || mcontactdb.iscontactexisted(key)) { continue; } contacts.add(new contactviewmodel(key, namemap.get(key), emailmap.get(key))); } return contacts; }
try below code fetch contact number of specific person.
contentresolver cr = getcontentresolver(); cursor cursor = cr.query(phone.content_uri, null, phone.display_name + "=?", new string[]{contactname}, null); if(cursor.getcount() > 0){ cursor.movetofirst(); { string number = cursor.getstring(mcursor.getcolumnindex(phone.number)); }while (cursor.movetonext() );
}
Comments
Post a Comment