articles

Home / DeveloperSection / Articles / Content Provider in Android

Content Provider in Android

Chris Anderson8694 15-Oct-2011

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

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

contacts.

Thanks.


Updated 07-Sep-2019
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By