ContentProvider are used to provide data from an application to another. ContentProvider do not store the data but provide the interface for other applications to access the data.

The following example will use an existing context provider from "Contacts".

Create contacts on your emulator

For this example we need a few maintained contacts. Select the home menu and

then the menu entry "Contacts" to create contacts.

Content Provider in Android

Press Menu and select "New Contact".

Content Provider in Android

As a result you should have a few new contacts.

Content Provider in Android

Using the Contact Content Provider

Create a new Android project "" with the activity


Rename the id of the existing TextView from the example wizard to "contactview".

Delete the default text. Also change the layout_height to "fill_parent".

The resulting main.xml should look like the following.

<?xml  version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
        android:orientation="vertical" android:layout_width="fill_parent"
        <TextView android:layout_width="fill_parent"
              android:layout_height="fill_parent" android:id="@+id/contactview" />

Access to the contact content provider requires a certain permission as not all applications should have access to the contact information. Open the AndroidManifest.xmland select the Permissions tab. On that tab click the "Add" button, and select "Uses Permission". From the drop-down list select the entry "android.permission.READ_CONTACTS".

Content Provider in Android

Change the coding of the activity.

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;

public class ContactsView extends Activity {
     /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
           TextView contactView = (TextView) findViewById(;
           Cursor cursor = getContacts();
            while (cursor.moveToNext())
              String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
              contactView.append("Name: ");

        private Cursor getContacts() {
               // Run query
              Uri uri = ContactsContract.Contacts.CONTENT_URI;
              String[] projection = new String[] { ContactsContract.Contacts._ID,
                           ContactsContract.Contacts.DISPLAY_NAME };
              String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"+ ("1") + "'";
              String[] selectionArgs = null;
              String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

               return managedQuery(uri, projection, selection, selectionArgs, sortOrder);

Right click on the project à Select Run As à Android Application.

Content Provider in Android

When you run your application it will show you all the names of the available



  Modified On Mar-23-2018 02:08:32 AM

Leave Comment