Content Providers in Android

This entry is part 30 of 50 in the series Android Development Primer

One of the most striking features of Android is Content Providers. In a sentence, Content Providers consist of a centrally located database in Android that helps applications use the data from other applications or those provided by Android itself. A very comprehensive article is on the official Android Documentation here.
This post is aimed at shoeing you how to use Content Providers if you need certain data in your application and hence I will not go into the details of what Content Providers are. Nevertheless, the article linked above will familiarize you with it to a good extent.

Now, we are going to create an application that extracts the contacts from the device. It is obvious that there is no way you can access the contacts because it belongs to a different application that comes pre-installed with Android. This is where we are going to use Content Providers. Let us see how:

  • The first thing we are going to do is add a permission to the manifest file of our application. The permission is android.permission.READ_CONTACTS.
  • This tells Android that we are going to access the contacts list of the device. You don’t want your application to use the contact list of a device without letting the user know. This is hence done to protect user privacy.
  • We are going to extract the contacts and send it to Logcat so we can check if our application ran succesfully.
  • We are not going to have a layout for this activity as we do not need it.
  • Add the following lines below the super.onCreate(savedInstanceState)

    Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while(c.moveToNext()){
            	int nameidx = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            	String name = c.getString(nameidx);
            	Log.d("CONTACTS", name);
            }
    
  • Save it and execute it on an emulator or a device.
  • Since there is no layout for this activity we are going to have to check out the DDMS for the Logs. Below is a snip of how it ran on my system. I have three contacs int my emulator- Nero, Vergil and Dante.

ContentP1

Understanding the Code:

  • Remember that the Content Providers are like a centrally located database.
  • A cursor can be considered analogous to a pointer in most programming languages. It points to the first of the rows retrieved from a query.
  • We access the Content Providers with the help of Content Resolvers class. Here, we make an inline query.
  • Each Content Provider is identified unquely by a URI i.e. Uniform Resource Identifier. The URI here is located with the help of ContactsContract class. The complete URI here is ContactsContract.Contacts.CONTENT_URI.
  • Since a cursor points to a complete row and we just need the name of the contact, we need to identify exactly which column will the contact name be in. For this we make use of the Column Index. We identify the Column Index by a particular constant string inside the PhoneLookup class i.e. PhoneLookup.DISPLAY_NAME. This name identifies uniquely the index of the column where the contact name is located.
  • Once we have the column index, we can easily extract the string from it and log it in the Logcat.
  • It must also be remembered that the Content Provider for this application is inbuilt in Android. If one wishes to share information between two standalone applications, one can create his own Content Provider. This would then enable all other applications to access the application’s data.

COMPLETE SOURCE CODE

MainActivity.java

package com.nero.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while(c.moveToNext()){
        	int nameidx = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);
        	String name = c.getString(nameidx);
        	Log.d("CONTACTS", name);
        }
    }
}

Series Navigation<< Network Access in Android ApplicationsList View using Array Adapters – Part I >>

Leave a Reply

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

*

* Copy This Password *

* Type Or Paste Password Here *

41,055 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>