How to remove a phone number from a contact in Android?

I want to delete (e.g. mobile phone number) from the Android database. For this, I pass the request following

public class ContactDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String number = "2222"; Long id = getID(number); int i = getContentResolver().delete(RawContacts.CONTENT_URI, RawContacts._ID+"=?", new String[]{id.toString()}); System.out.println("Deleted"+i); } public Long getID(String number){ Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); Cursor c = getContentResolver().query(uri, new String[]{PhoneLookup._ID}, null, null, null); while(c.moveToNext()){ return c.getLong(c.getColumnIndex(PhoneLookup._ID)); } return null; } } 

but he removes all contact.

What should I use to delete only this phone number (not full contact)?

+4
source share
1 answer

You use the ContentResolver removal method to delete the entire contact. To update the phone number of this contact to an empty value, you need to use the ContactsContact API.

http://developer.android.com/reference/android/provider/ContactsContract.Data.html

Providing the identifier of the raw contact and Phone.CONTENT_ITEM_TYPE, you can only request the phone number belonging to this contact, and then delete all of them.

  ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI) .withSelection(Data._ID + "=? and " + Data.MIMETYPE + "=?", new String[]{String.valueOf(contactId), Phone.CONTENT_ITEM_TYPE}) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
+6
source

Source: https://habr.com/ru/post/1336950/


All Articles