android以编程方式删除特定联系人

时间:2012-06-12 16:03:15

标签: android

现在,这段代码正常运行 ops.add(ContentProviderOperation .newDelete(ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data.RAW_CONTACT_ID+"=?",new String[] {sid}).build());

然而,它创建了未知记录,似乎是已删除的联系人。我需要一些东西让它正常工作吗?

3 个答案:

答案 0 :(得分:8)

我以这种方式工作:

public static boolean deleteContact(Context ctx, String phone, String name) {
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    Cursor cur = ctx.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);
                    ctx.getContentResolver().delete(uri, null, null);
                    return true;
                }

            } while (cur.moveToNext());
        }

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

答案 1 :(得分:0)

中所述

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

RawContacts中的

CONTACT_ID字段是聚合联系人的链接。通过ContentProviderOperation.newDelete删除RawContact将使该字段为空(正如我所发现的)。实际删除RawContact时,最多可以进行Android同步和聚合。我需要删除特定帐户中的特定联系人,因此通过RawContacts删除,而不是汇总的联系人为我工作。要检查是否已删除RawContact,我使用了:

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

    ContentResolver contentResolver = getContentResolver();

    Cursor curRawContacts = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, null,
            ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND " + ContactsContract.RawContacts.ACCOUNT_NAME
                    + " = ?", new String[]{"MYTYPE", MyAccount}, null);

    int size = curRawContacts.getCount();


    for (int i = 0; i < size; i++) {

        curRawContacts.moveToPosition(i);

        //getColumnIndexOrThrow(String columnName)
        //Returns the zero-based index for the given column name, or throws IlleidRawContactgalArgumentException if the column doesn 't exist.
        String idRawContact = curRawContacts.getString(curRawContacts.getColumnIndexOrThrow(ContactsContract.RawContacts._ID));
        String idContact = curRawContacts.getString(curRawContacts.getColumnIndexOrThrow(ContactsContract.RawContacts.CONTACT_ID));

        // if link to aggregated contact is null, than raw contact had already been deleted
        if (idContact != null) {

答案 2 :(得分:0)

ContentProvider

newDelete方法对我不起作用。它会删除联系人中的所有信息,但会在电话簿中留下空(null null)联系人。

这就是为什么我必须使用 ContentResolver delete方法。

我想删除使用特定SOURCE_ID我的应用创建的联系人。

我已将CALLER_IS_SYNCADAPTER设为true

所以这就是我管理它的方式:

Uri contactUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
String whereClause = RawContacts.SOURCE_ID + " = ?";
String[] args = new String[]{contactId};

int deletedRawContacts = context.getContentResolver().delete(contactUri, whereClause, args);

if (deletedRawContacts > 0) {
    Log.d(TAG, "Delete OK");
} else {
    Log.d(TAG, "Nothing to delete");
}