Home > DeveloperSection > Articles > Content Provider in Android

Content Provider in Android

Android Android 
0 Comment(s)
 5776  View(s)
Rate this:

Content Provider in Android

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 "com.android.contentprovider" with the activity "ContactsView".

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="http://schemas.android.com/apk/res/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.xml, and 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.

package com.android.contentprovider;


import android.app.Activity;

import android.database.Cursor;

import android.net.Uri;

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(R.id.contactview);

          Cursor cursor = getContacts();

          while (cursor.moveToNext())


              String displayName = cursor.getString(cursor     

              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,




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 contacts.


Don't want to miss updates? Please click the below button!

Follow MindStick