Content Provider

Every application has its own sandbox and cannot access data from other applications. If access to functions not provided by its own sandbox is required, the application must explicitly declare permission upfront before installation. Android provides an interface called ContentProvider to act as a bridge between applications, enabling them to share and change each other's data.A content provider allows a clean separation between the application layer and data layer. It requires a permission setting in the AndroidManifest XML file and can be accessed using a simple URI model.

Some native databases Android makes available as content providers are

■ Browser—Read or modify bookmarks, browser history, or web searches.

■ CallLog—View or update the call history.

■ Contacts—Retrieve, modify, or store the personal contacts. Contact information is stored in a three-tier data model of tables under a ContactsContract object:

■ ContactsContract.Data—Contains all kinds of personal data.There is a predefined set of common data, such as phone numbers and email addresses, but the format of this table can be application-specific.

■ ContactsContract.RawContacts—Contains a set of Data objects associated with a single account or person.

■ ContactsContract.Contacts—Contains an aggregate of one or more Raw-Contacts, presumably describing the same person.

■ LiveFolders—A special folder whose content is provided by a ContentProvider.

■ MediaStore—Access audio, video, and images.

■ Setting—View and retrieve Bluetooth settings, ring tones, and other device preferences.

■ SearchRecentSuggestions—Can be configured to operate with a search suggestions provider.

■ SyncStateContract—ContentProvider contract for associating data with a data array account. Providers that want to store this data in a standard way can use this.

■ UserDictionary—Provides user-defined words used by input methods during predictive text input.Applications and input methods can add words to the dictionary. Words can have associated frequency information and locale information.

To access a content provider, the application needs to get a contentResolver instance to query, insert, delete, and update the data from the content provider, as shown in the following example:

ContentResolver crInstance = getContentResolver(); //get a content Resolver instance crInstance.query(People.CONTENT_URI, null, null, null, null); //query contacts ContentValues new_Values= new ContentValues();

crInstance.insert(People.CONTENT_URI, new_Values); // insert new values crInstance.delete(People_URI, null, null); //delete all contacts

ContentValues update_Values= new ContentValues();

crInstance.update(People_URI, update_Value, null,null); //update values

Each content provider needs to have a Uniform Resource Identifier (URI), which is used for registration and permission access. The URI must be unique between providers and have the generic suggested format:

content://<package name>.provider.<custom ContentProvider name>/<DataPath>

For simplicity, it can also be just content://com.cookbook.datastorage/diaries, which is used in the next recipe.The Urimatcher is utilized in the ContentProvider to ensure a proper URI is passed.

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