Recipe Creating a Custom Content Provider

After getting a sense of how to use a content provider, it is time to integrate one into the diary project used in previous recipes.This recipe shows how to expose diary entries to other selected applications.A custom content provider just extends the Android ContentProvider class, which contains six methods to optionally override:

■ query()—Allows third-party applications to retrieve content.

■ insert()—Allows third-party applications to insert content.

■ update()—Allows third-party applications to update content.

■ delete()—Allows third-party applications to delete content.

■ getType()—Allows third-party applications to read each of URI structures supported.

■ onCreate()—Creates a database instance to help retrieve the content.

For example, if other applications are allowed to read only content from the provider, just onCreate() and query() need to be overridden.

A custom ContentProvider is shown in Listing 9.18; it has one URI added to UriMatcher based on the package com.cookbook.datastorage and the database table name diaries.The onCreate() method forms a MyDB object with code in Listing 9.9. It is responsible for the database access. The query() method retrieves all records from the diaries database, which is passed as the uri argument. In case of a more specific selection of records, the other arguments of this method would be utilized.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment