user173488
user173488

Reputation: 947

android how to tag phone number of a contact programmatically

i did a lot of research but couldn't find anything to help;

my problem is: i need to create an application where the user can select some of the contacts on his phone to be added to this application where he/she can later communicate with them via sms in a special template. but the user need to select only one phone number to be active to this user on this application. this choice must be caved for later logons.

i was able to retrieve contacts and their phone numbers using lookupkey (which will be saved in my application as a reference for preselected users), but i couldn't figure out how to tag the needed phone number, i was thinking of adding a flag to the phone number but i dont know how, i dont know if this is the right way to do it, i thought of setting the selected phone number as primary then query t when needed... or simply save the phone number id (but i am not sure if saving the id is safe in case user changed the phone numer)...

thx for any help...

Upvotes: 0

Views: 908

Answers (2)

Michael Hamilton
Michael Hamilton

Reputation: 586

I'm a bit new to all this stuff. As I understand it, an id match is fast, but may not be stable. If a key+id match fails, you should also try a slower match using only the key - Jeffrey Scofield's SQL selection string could be changed to try id-and-key OR key-only (trusting the query optimiser to prioritize the id match).

I haven't had much luck finding information concerning the wisdom of storing the key long term.

Upvotes: 0

user173488
user173488

Reputation: 947

After a long period of trial and error I found the solution to my problem. I will be using the contacts lookup key to store the contact and the phone id to store the phone number...as follows:

String selection =  ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY + " = '" + lookupkey+ "' and "+Phone._ID+"='"+phoneid+"'";
        String[] projection =new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};
         Cursor managedCursor = getContentResolver()
                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                 projection, selection, null,  Phone.DISPLAY_NAME + " ASC");

Upvotes: 1

Related Questions