Thursday, April 1, 2010

Content Provider Example - 1

In Android, a content provider is a specialized type of data store that exposes standardized ways to retrieve and manipulate the stored data.

You wish to share data between applications, you need to use the content provider model as recommended in Android. This article presents the basics of content providers and how you can implement one.

We are going to discuss following item here
  1. Using a Content Provider
  2. Create a Custom Content Provider

Using a Content Provider
Here are some of Android's most useful built-in content providers
Content Provider     Intended Data
Contacts                Contact details
Browser                 Browser bookmarks, browser history, etc.
CallLog                   Missed calls, call details, etc.
MediaStore             Media files such as audio, video and images
Settings                 Device settings and preferences

Here are the sample method for accessing the Contacts Content Provider.

public void readContact() {   
        ArrayList<String> contactList;
        contactList = new ArrayList<String>();

        String[] columns = new String[] { People.NAME, People.NUMBER };
        Uri mContacts = People.CONTENT_URI;
        Cursor mCur = managedQuery(mContacts, columns, null, null, People.NAME+ " ASC ");
        if (mCur.moveToFirst()) {
            do {
                contactList.add(mCur.getString(mCur.getColumnIndex(People.NAME)));
            } while (mCur.moveToNext());
        }
        Toast.makeText(this, contactList.size() + "", Toast.LENGTH_LONG).show();
    }



Create a Custom Content Provider
1. Create a Content Provier Class
2. Override the Following Methods
3. Declare the Constant Content Provider Values.
4. Implement the business login in the override methods.
5. Create a Database Helper to Create database Schema
6. Accessing Our Content Provider

Create a Content Provier Class
    Create a customized class which extend the ContentProvider Class. ContentProvider is an abstract Class, so we need to override the method in the content provider class.
        public class MyContentProvider extends ContentProvider{}

Override the Following Methods

    * onCreate(): Called when the provider is being started.
    * getType(): Returns the MIME type of the data at the given URI.       

    CRUD Operation
    * insert(): Inserts a new record into the content provider.
    * query(): Receives a request from a client. The result is returned as a Cursor object.
    * update(): Updates an existing record from the content provider.
    * delete(): Deletes an existing record from the content provider.          

   
   
Declare the Constant Content Provider Values.

1. Provider Name - Which is used to access the content provider.
        1. Standard prefix indicating that the data is controlled by a content provider. IT'S NEVER MODIFIED.
        2.The authority part of the URI; it identifies the content provider. For third-party applications, this should be a fully-qualified class name (reduced to lowercase) to ensure uniqueness. The authority is declared in the <provider> element's authorities attribute:
            <provider name=".TransportationProvider" authorities="com.example.transportationprovider"  . .  >
        3. The path that the content provider uses to determine what kind of data is being requested. This can be zero or more segments long. If the content provider exposes only one type of data (only trains, for example), it can be absent. If the provider exposes several types, including subtypes, it can be several segments long for example, "land/bus", "land/train", "sea/ship", and "sea/submarine" to give four possibilities.
        4. The ID of the specific record being requested, if any. This is the _ID value of the requested record. If the request is not limited to a single record, this segment and the trailing slash are omitted:

Example
         public static final String PROVIDER_NAME = "com.contentproviderexample.mycontentprovider"; // Same as Androidmanifest.xml entry
         public static final Uri CONTENT_URI = Uri.parse("content://"+ PROVIDER_NAME + "/demodb"); // URI for access the Content Provider


 2. Create a VO for Table with some content Provider Value
        For example i have create a table userinfo which contains the two column [id and isalive]. i have created the following VO class

 public static final class UserInfo implements BaseColumns {
            public static final String DATABASE_TABLE = "userinfo";
            public static final int USERINFO = 1;
            public static final int USERINFO_ID = 2;

            public static final Uri CONTENT_URI = Uri.parse("content://"
                    + PROVIDER_NAME + "/userinfo");
            public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.contentproviderexample.userinfo";
            public static fin
al String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.contentproviderexample.userinfo";

            public static final String _ID = "_id";
            public static final String isactive = "isactive";
        }


 3.Create  a object for UriMatcher. and add the our URL with UriMatcher

        static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, UserInfo.DATABASE_TABLE,UserInfo.USERINFO);
        uriMatcher.addURI(PROVIDER_NAME, UserInfo.DATABASE_TABLE + "/#",UserInfo.USERINFO);
        }




No comments:

Post a Comment