While developing my app DCM I used both the ContentResolver and the CursorLoader to fetch contacts from the mobile. The ContentResolver is a class that provides applications access to the content model. The CursorLoader is a loader object that queries a ContentResolver for data. In my trials I noticed that the ContentResolver is much faster.
This application compares the execution time of both methods in fetching contacts. The library is modified and extended from my Mobile-Contacts-Library which solely used the ContentResolver to fetch contacts:
The following images are results of a test performed on the simulator. Real life tests on my Samsung tablet S2 showed that the ContentResolver was almost twice faster than the CursorLoader.
The demo app for this library is available on Google Play:
In the app manifest file add this permission:
<uses-permission android:name="android.permission.READ_CONTACTS" />
In the app build.gradle add the following:
a. Add JitPack repository at the end of repositories
repositories
{
maven { url 'https://jitpack.io' }
}
b. Add the dependency
dependencies
{
compile 'com.github.Jagerfield:Resolver-Vs-Loader:v1.3'
}
You can use this library to fetch contacts using either one of the methods or both:
new ImportContacts(getActivity(), new ImportContacts.ContentResolverCallback()
{
@Override
public void getMobileContacts(ArrayList<Contact> contacts)
{
if (contacts != null)
{
//Write your code here
}
}
});
new ImportContacts(getActivity(), new ImportContacts.CursorLoaderCallback()
{
@Override
public void getMobileContacts(ArrayList<Contact> contacts)
{
if (contacts != null)
{
//Write your code here
}
}
});
This app sample will request the follwoing permission below which require user approval and is used for demonstration purposes only. No data is shared or used outside this app.
- android.permission.READ_CONTACTS