---
title: "Content Provider in Android"  
description: "ContentProvider are used to provide data from an application to another. ContentProvider do not store the data but provide the interface for other app"  
author: "Chris Anderson"  
published: 2011-10-15  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/781/content-provider-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Content Provider in Android

[ContentProvider](https://www.mindstick.com/interview/12742/what-is-a-contentprovider-in-android-and-what-is-it-typically-used-for) are used to provide data from an application to another. ContentProvider do not store the data but provide the [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) for other [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications) to [access the data](https://answers.mindstick.com/qa/40056/what-is-the-weo-update-and-how-can-i-access-the-data).\

The following example will use an existing [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) provider from "Contacts".

**Create [contacts on your](https://answers.mindstick.com/qa/50718/how-to-add-import-and-delete-contacts-on-your-iphone-8-plus) 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](https://www.mindstick.com/mindstickarticle/70072812-e6d0-41e4-89ab-da670d747a44/images/df26db92-9a38-4de0-ac05-367d74965db4.png)

Press Menu and select "New Contact".

![Content Provider in Android](https://www.mindstick.com/mindstickarticle/70072812-e6d0-41e4-89ab-da670d747a44/images/9f378673-e2bd-405b-8ef7-e06f1f3e7734.png)

As a result you should have a few new contacts.

![Content Provider in Android](https://www.mindstick.com/mindstickarticle/70072812-e6d0-41e4-89ab-da670d747a44/images/3e57eda3-8c09-43a7-9a46-d74fdca0110b.png)

##### 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"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
              android:layout_height="fill_parent" android:id="@+id/contactview"  />
 </
 </LinearLayout>
```

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](https://answers.mindstick.com/qa/33390/how-to-enable-api-access-in-salesforce-by-permission-set)". From the drop-down list select the entry "[android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android).permission.READ_CONTACTS".

![Content Provider in Android](https://www.mindstick.com/mindstickarticle/70072812-e6d0-41e4-89ab-da670d747a44/images/e28452f6-1a50-4825-beda-6553a7c10dfe.png)

##### 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. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           TextView contactView = (TextView) findViewById(R.id.contactview);
           Cursor cursor = getContacts();
            while (cursor.moveToNext())
   {
              String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
              contactView.append("Name: ");
              contactView.append(displayName);
              contactView.append("\n");
          }
       }

        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](https://www.mindstick.com/mindstickarticle/70072812-e6d0-41e4-89ab-da670d747a44/images/5f649d80-baa2-4599-95f6-828241f2e3e1.png)

When you run [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding) it will show you all the names of the available

contacts.

Thanks.

---

Original Source: https://www.mindstick.com/articles/781/content-provider-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
