Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:05:11+00:00 2026-05-27T20:05:11+00:00

I am working on Unit Tests for my Android app, and am doing a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:05:11+00:00Added an answer on May 27, 2026 at 8:05 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on unit tests for my Rails app and have run into the
I'm working on a Spring MVC project, and I have unit tests for all
I am a university student. I have been working a lot with Android lately
I'm trying to get into unit tests for a BizTalk application I'm working on,
I am working on a Java web app with unit tests that deploy the
I'm working on an application and I have created a number of unit tests
I am working with a unit-testing suite that hijacks function calls and tests expected
I am working to integrate unit testing into the development process on the team
I have unit tests set up for my iPhone project built using the Google
I'm working on unit-tests for an application which has a constructor that takes three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.