Home » Android » How to update contact number using Android

How to update contact number using Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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 });        
How to&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);
    }

Answer:

Try this

String where = ContactsContract.Data.DISPLAY_NAME + "=? AND " + 
    ContactsContract.Data.MIMETYPE + "=? AND " + 
    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + "=?";

String[] params = new String[] {
    Cname,
    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
    String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
};

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
    .withSelection(where, params)
    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"9999999999")
    // .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name 21")
    .build()
);

//email
String where3 = ContactsContract.Data.DISPLAY_NAME + " = ? AND " + 
                                ContactsContract.Data.MIMETYPE + " = ?";

String[] params3 = new String[] {
    Cname,
    "vnd.android.cursor.item/email_v2"
};

ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
    .withSelection(where3, params3)
    .withValue(ContactsContract.CommonDataKinds.Email.DATA,"[email protected]")
    .build()
);

By using this I am able to update the contact details.

Answer:

Just small change, if you need to update some other phone than work phone, you can have method that has this as a parameter.

public void updateContact (String contactId, String newNumber, String phoneType) 
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, phoneType}; 
    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(selectPhone, phoneArgs)
            .withValue(Phone.NUMBER, newNumber)
            .build()); 
    this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}

Answer:

This class may help to update the contact.

public class UpdateContact extends Activity {

    private EditText txtName,txtPh;
    private Button btnUpdate;
    private TextView txt;
    private static final String TAG_ID="id";
    private static final String TAG_NAME = "name";
    private static final String TAG_PHNO = "phNo";
    private static int id;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_contact);
        txtName=(EditText)findViewById(R.id.txtNname);
        txtPh=(EditText)findViewById(R.id.txtNPhNo);
        btnUpdate=(Button)findViewById(R.id.btnUpdate);
        txt=(TextView)findViewById(R.id.txtId);

        Intent i=getIntent();
        id=i.getIntExtra(TAG_ID,0);
        txtName.setText(i.getStringExtra(TAG_NAME));
        txtPh.setText(i.getStringExtra(TAG_PHNO));
        //to update contact on button click you can apply this
        btnUpdate.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DatabaseHandler handler=new DatabaseHandler(UpdateContact.this);
                Contact contact=new Contact();
                contact.setID(id);
                contact.setName(txtName.getText().toString());
                contact.setPhNo(txtPh.getText().toString());
                handler.UpdateContact(contact);
                finish();
            }
        });

    }  
}