android - Mismatching contact id's while trying to blacklist phonenumbers -
i'm using contact picker below id of contact.
public void pickcontact() { intent intent = new intent(intent.action_pick, contacts.content_uri); intent.settype(phone.content_type); // show user contacts w/ phone numbers startactivityforresult(intent, pick_contact_request); }
then retrieve contact id uri returned above using this. , store reference.
public static long getcontactidbyuri(context context, uri uri) { log.d(tag, uri.tostring()); string[] projection = { contacts._id }; cursor cursor = context.getcontentresolver().query(uri, projection, null, null, null); try { cursor.movetofirst(); int idx = cursor.getcolumnindex(contacts._id); long id = -1; if(idx != -1) { id = cursor.getlong(idx); } return id; } { cursor.close(); } }
later on when text message arrives fetch phone number , based on try contact id using following.
public static long getcontactidbyphonenumber(context context, string phonenumber) { contentresolver contentresolver = context.getcontentresolver(); uri uri = uri.withappendedpath(phonelookup.content_filter_uri, uri.encode(phonenumber)); string[] projection = new string[] { phonelookup._id }; cursor cursor = contentresolver.query(uri, projection, null, null, null); if (cursor == null) { return -1; } int idx = cursor.getcolumnindex(phonelookup._id); long id = -1; if(cursor.movetofirst()) { id = cursor.getlong(idx); } if(cursor != null && !cursor.isclosed()) { cursor.close(); } return id; }
the problem 2 id's doesn't match!
so question goes how can id contact picker can match when looking phone number phonelookup.content_filter_uri. can use additional information contact?
the url returned contact picker refers contactscontract.data provider joined contactscontract.rawcontacts again contains contact_id.
so extracting actual contact id trivial using method below.
public static long getcontactidbydatauri(context context, uri uri) { string[] projection = new string[] { data.contact_id }; cursor cursor = context.getcontentresolver().query(uri, projection, null, null, null); long id = -1; if(cursor.movetofirst()) { id = cursor.getlong(0); } return id; }
Comments
Post a Comment