Android Read Contacts using Content Provider - Androhub

Read Contact

Android Read Contacts using Content Provider

You can read phone contacts using Content Provider. With the help of this content provider we can communicate with the contacts app and query the URIs  and columns to get contact information and it will return as contact name, all contact numbers, all email addresses, company name, etc.

Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don’t need to share data amongest multiple applications you can use a database directly via SQLiteDatabase.

When you want to access data in a content provider, you use the ContentResolver object in your application’s Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.

Example

In this tutorial, we are going to learn how to read contact details using content provider.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Now, to read contact details we have to give “READ_CONTACTS” this permission in our Manifest file.

3. Create a xml layout naming activity_main.xml that contains one listview that will display the contact details.

4. Now, create a new xml layout naming custom_view.xml for custom listview layout. Since we have to display contact details like name, number, email, image and other details so we are using custom view.

If you are new to custom list view then you can read here.

5. Now, create a new java class naming Contact_Model.java for getter and setter for contact details.

6. Now, create a custom adapter class naming Contact_Adapter.java for inflating a custom layout over listview.

7. To read contact details we have to know certain things mentioned below :

  • URI that we require in querying.

  •  Fetching contact details into cursor in ascending order. using uri

  • Now we will use this method to fetch data and stored into arraylist.

8. Finally open your MainActivty.java and add the following code. In this class we fetch contact details using AsyncTask and display over listview.

9. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

 

4 Comments

sravani
Monday, March 26th, 2018

Hi , i want source code of how to retrieve a list of contacts and then how to send and receive sms in android stuio

Sujeet
Thursday, October 25th, 2018

Thank you so much..

shayan
Wednesday, April 10th, 2019

my app is crash when it run

Dr. Droid
Thursday, April 11th, 2019

Hi Shayan,

Can you share your error logs?

Thanks

Post comment

Your email address will not be published. Required fields are marked *