在 Android 2.x 中删除(原始)联系人

发布于 2024-12-03 09:14:09 字数 3238 浏览 0 评论 0原文

我正在为我的 Android 应用程序进行单元测试,并且在联系人方面做了很多工作。我必须将联系人插入 Android 内容提供程序,并在运行测试后删除它们。问题是,它们并没有被实际删除:

插入:

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

int backRefIndex = 0;

Random r = new Random();

    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                                           .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                                           .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
                                           .build());      
    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                           .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
                                           .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                                           .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name" + r.nextInt())
                                           .build());

    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
                       .withValue(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "020" + r.nextInt())
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, r.nextInt(20)
                       .build());

    try {
        ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, contactOps);
    } catch (Exception e) {
        e.printStackTrace();
    }

删除方法 1(返回原始联系人数量,但实际上并未被删除):

int deletedRawContacts = context.getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + " >= ?", new String[]{"0"});

删除方法 2(与删除方法 1 的结果相同,但是不同的方法):

private static int deleteAllRawContacts(Context context) {
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.RawContacts.CONTENT_URI, null, null, null, null);
    int count = 0;
    while (cur.moveToNext()) {
        try {
            String contactId = cur.getString(cur.getColumnIndex(ContactsContract.RawContacts._ID));
                count += cr.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + " = ?", new String[]{contactId});
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
    }
    return count;
}

联系人的删除方法有效,但原始联系人的删除方法将返回错误值。它会“告诉”我,它删除了所有联系人,但是当我运行下一个测试用例时,仍然可以找到旧的原始联系人(即插入的联系人与当前联系人的计数是错误的)。注意:所有测试均在 Android 模拟器上完成。

有什么想法如何解决这个问题吗?

我在这里看到了类似的问题:如何删除联系人?< /a> - 但该解决方案似乎也没有解决给定的问题。

I am working on Unit Tests for my Android app, and am doing a lot with Contacts. I have to insert contacts into the Android Content Providers, and delete them after running my tests. Trouble is, they do not get actually deleted:

Insertion:

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

int backRefIndex = 0;

Random r = new Random();

    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                                           .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                                           .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
                                           .build());      
    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                           .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
                                           .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                                           .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Sample Name" + r.nextInt())
                                           .build());

    contactOps.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, backRefIndex)
                       .withValue(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "020" + r.nextInt())
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, r.nextInt(20)
                       .build());

    try {
        ContentProviderResult[] result = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, contactOps);
    } catch (Exception e) {
        e.printStackTrace();
    }

Deletion method 1 (returns number of raw contacts, but they do not actually get deleted):

int deletedRawContacts = context.getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + " >= ?", new String[]{"0"});

Deletion method 2 (same result as deletion method 1, but different approach):

private static int deleteAllRawContacts(Context context) {
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.RawContacts.CONTENT_URI, null, null, null, null);
    int count = 0;
    while (cur.moveToNext()) {
        try {
            String contactId = cur.getString(cur.getColumnIndex(ContactsContract.RawContacts._ID));
                count += cr.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + " = ?", new String[]{contactId});
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
    }
    return count;
}

The deletion method for Contacts works, but the deletion method for Raw Contacts will return a false value. It will "tell" me, that it deleted all contacts, but when I run my next test case, the old Raw Contacts can still be found (i.e. the count of inserted contacts vs. present contacts is wrong). Note: All testing is done on the Android emulator.

Any ideas how to solve this?

I saw a similar question here: How to delete a contact? - but the solution does not seem to solve the given problem either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-12-10 09:14:09

正如wiseideal已经提到的,删除rawcontacts的方式只会将“已删除”标志设置为1。

您需要做的是将URI中的caller_is_syncadapter标志设置为true,如下所示:

RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build()

然后使用这个新的URI来调用删除方法:

int deletedRawContacts = context.getContentResolver().delete(RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(), ContactsContract.RawContacts._ID + " >= ?", new String[]{"0"});

文档中对应的部分是此处(操作->删除)。

希望这对您有所帮助并祝您编码愉快:)

As wiseideal already mentioned the way you delete your rawcontacts will only set the "deleted"-flag to 1.

What you need to do is to set the caller_is_syncadapter-flag in your URI to true like this:

RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build()

And then use this new URI to call the delete-method:

int deletedRawContacts = context.getContentResolver().delete(RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(), ContactsContract.RawContacts._ID + " >= ?", new String[]{"0"});

The corresponding part in the documentation is here (Operations->delete).

Hope this helps and happy coding :)

み青杉依旧 2024-12-10 09:14:09

我正在研究同样的问题。我发现一旦“删除”它,删除列就会设置为 1。所以我认为 contentresolver 不会物理删除原始联系人数据,它只是设置一个删除标志。也许我们应该避免这些标签。

I am working on the same issue.I found the delete column is setted to 1 once I "delete" it.So I think contentresolver doesnt delete the rawcontact data physically,it just set a delete flag.Maybe we should avoid these tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文