Search code examples
androidandroid-contentproviderandroid-contacts

Android native contact modification


I'm new to android and i'm working with native contact.

So my app is let user put contact display name and their number for edit/delete.

In case the contact have more that one number.

I tried a lot but still have no luck, the app still doesn't update the number or it crashes.

What I'm going to do as my understanding is:

  1. Find name in contact that matched name user inserted and use that to get contact_id that represent this contact datagroup.
  2. Use contact_id in 1. and the number user input to find ._ID that represent the specific row id.
  3. Do task with ._ID we get from 2.

This is 1. code to get contact_id:

 public String getPeopleUniqueID(String name, Context context) {
    String s = null;
            String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
    String[] projection = new String[] {ContactsContract.Data.CONTACT_ID};
    Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            projection, selection, null, null);
    if(c.moveToFirst()) {

    s = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
    }
    c.close();
    return  s;
}

This is 2. code to get ._ID (num is number user inserted and name is from 1. > the contact_id)

 public String checkPhoneNumber(String num, String name, Context context) {
    String s = null;
    String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + "=?" + " AND "+ContactsContract.Data.CONTACT_ID+ "=?";
    String[] projection = new String[] {ContactsContract.Data._ID};
    Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            projection, selection, new String[]{u,name}, null);
    if(c.moveToFirst()) {

        s=c.getString(c.getColumnIndex(ContactsContract.Data._ID));
    }
    c.close();
      if (s==null){
          s = "null";
      }
    return s;
}

To do something like editing (num is _.ID we get from 2. and newnum is new number user want to change into).

 public void editNumber(String num , String newnum) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(Data._ID + "=? AND " +
                            Data.MIMETYPE + "='" +
                            CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'",
                    new String[]{num})
            .withValue(Data.DATA1, newnum)
            .build());

    try{
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);}
    catch (RemoteException e){e.printStackTrace();}catch (OperationApplicationException e) {e.printStackTrace();}
}

And well it crashes when I call editNumber().

Can you help me fix my code and my understanding?

And another question, can I edit/insert group for the contact programatically, like I want to add this contact to family friend or co-worker group (the default group that we can set at contact edit page)?


Solution

  • Use ContactsContract.Contacts.CONTENT_FILTER_URI for searching a contact based on name - to get Id or anything else. The like operator cannot handle all cases which the CONTENT_FILTER_URI does handle - For various languages, special characters etc. http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_FILTER_URI

    Use following uri to lookup a contact from phone number - you can get person id or anything else :

    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
         Uri.encode(phoneNumber));
    

    In the set query you can also use contactId in the condition

    For groups you can use custom mimetypes if the default one does not suit you (which is still very primitive for groups across different account types)