7

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

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

4

3 回答 3

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;
}
于 2012-11-15T07:20:07.010 回答
0

如中所述

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

RawContacts 中的 CONTACT_ID 字段是聚合联系人的链接。通过删除 RawContactContentProviderOperation.newDelete将使该字段为空(正如我发现的那样)。何时实际删除 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) {
于 2015-12-14T12:44:18.957 回答
0

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

这就是为什么我必须将ContentResolverdelete方法一起使用。

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

我已经设置CALLER_IS_SYNCADAPTERtrue.

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

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");
}
于 2017-01-24T10:03:10.683 回答