How to delete a particular contact using contact id?

前端 未结 4 360
迷失自我
迷失自我 2020-12-17 06:39

I am trying to delete a particular contact from phone. I can delete the full contact. How to delete a particular contact using contact id. I want to delete the full datas in

4条回答
  •  抹茶落季
    2020-12-17 06:54

    public void deleteContact(Context context, String localContactId)
    {
        ContentResolver cr = context.getContentResolver();
        String rawWhere = ContactsContract.Contacts._ID + " = ? ";
        String[] whereArgs1 = new String[]{localContactId};
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, rawWhere, whereArgs1, null);
    
        if(cur != null && cur.getCount() > 0) {
            while (cur.moveToNext()) {
                try{
                    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                    cr.delete(uri, null, null);
    
                }
                catch(Exception e)
                {
                    System.out.println(e.getStackTrace());
                }
            }
        }
        if(cur != null)
            cur.close();
    }
    

提交回复
热议问题