Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update contact number using Android

Tags:

android

I am learning android. I am trying to upadate contact number programmatically. Could anyone help me please how can I do that.

My effort is:

String lNumber = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));   

ContentValues values = new ContentValues();

Uri lPhoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.NUMBER);                      

values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "45323333"));
getContentResover().update(lPhoneUri, values, ContactsContract.CommonDataKinds.Phone.NUMBER+"=?", new String[] { lNumber });        
like image 415
Nick Avatar asked Jul 28 '10 09:07

Nick


People also ask

How do I update my mobile contacts?

If Automatically sync is turned off and you want to update your contacts, you can manually sync Google contacts with your device using Google's Contacts app. On your Android phone or tablet, open the Contacts app . To sync, pull down on the contacts list.


1 Answers

I think you are pretty much there. The following uses the new API to update the WORK phone number of a contact, assume that that contact already has a work phone number.

public void updateContact (String contactId, String newNumber, Activity act) 
    throws RemoteException, OperationApplicationException{

        //ASSERT: @contactId alreay has a work phone number 
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
        String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + 
                        Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
        String[] phoneArgs = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)}; 
        ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withSelection(selectPhone, phoneArgs)
                .withValue(Phone.NUMBER, newNumber)
                .build()); 
        act.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
like image 179
Paul Hoang Avatar answered Oct 13 '22 22:10

Paul Hoang