2015-04-08 173 views
0

我想从我的联系人列表中永久删除联系人。 我正在使用以下代码。如何从联系人列表中删除联系人号码?

public boolean deleteContact(String phone, String name) { 
    System.out.println("name :: "+name +" "+phone); 
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(phone)); 
    Cursor cur = context.getContentResolver().query(contactUri, null, null, 
      null, null); 
    try { 
     if (cur.moveToFirst()) { 
      do { 
       if (cur.getString(
         cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)) 
         .equalsIgnoreCase(name)) { 
        String lookupKey = cur 
          .getString(cur 
            .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
        Uri uri = Uri.withAppendedPath(
          ContactsContract.Contacts.CONTENT_LOOKUP_URI, 
          lookupKey); 
        int i = context.getContentResolver().delete(uri, null, null); 
        System.out.println("i :::: "+i); 
        return true; 
       } 

      } while (cur.moveToNext()); 
     } 

    } catch (Exception e) { 
     System.out.println(e.getStackTrace()); 
    } 
    return false; 
} 

在我的移动联系人中,我有一个名为“SIRI”的联系人,有两个手机号码。上面的代码删除了两个数字。我想只删除选定的号码,而不是两个号码。

回答

0

如果你只有一个要删除所选的电话号码,试试这个:

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone)); 
resolver.delete(uri, null, null); 

简单尽一切一气呵成:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 

String selections = ContactsContract.CommonDataKinds.Phone.NUMBER + " = ' + phone + ' AND " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME.toLowerCase() + " = ' + name.toLowerCase() + '"; 

int res = contentResolver.delete(uri, selections, null); 
+0

如果有两个数字,名字的意思是我想删除选定的号码不是名称和其他号码也。感谢您的回复。 – Siri

相关问题