MY mENU


Tuesday, 14 February 2012

ContentProvider in Android

Content Provider Component:
       when we created a sqlite database android application. We saw that the database file is stored in the file system directories of that application meaning that the database cannot be accessed from another application. You can apply the same thing on any resources within your application (Files, images, videos). But there is a method to expose your data across multiple applications: Content Providers.

Content Providers makes a specific set of the applications data available to other applications
you can use content providers to store or retrieve data of one application from any other application. Content Provider usually the backend is SQLite. They are loosely linked to clients.  
You can think of content providers as a database server. A content provider's job is to manage access to persisted data, such as a SQLite database. there's no common storage area that all Android packages can access. 
                                                
 If your application is very simple, you might not necessarily create a content provider. If you're building a larger application, or one that makes data available to multiple activities or applications, a content provider is the means of accessing your data. A content provider is implemented as a subclass of Content Provider. We can modify the data with the help of Content Provider.

Android default content providers:There are many built-in content providers supplied by OS. They are defined in the android.provider package, they include:

  •          Browser.
  •          Call log.
  •          Live Folders.
  •          Contacts Contract.
  •          Media Store.
  •          Settings
public abstract class ContentProvider extends Object implements ComponentCallbacks
Note :- Content providers are also useful for reading and writing data that is private to your application and not shared. if you want to share data. You have two options:
     1.  You can create your own content provider and extends the ContentProvider class
     2.  You can add the data to an existing provider and gives permission to write to it.

Creating a Content Provider :- Firstly, you will set up a system for storing the data.You can store your data any way you want in SQLite database SQLiteOpenHelper class is helpful for creation of a database and SQLiteDatabase to manage it. This class extend the ContentProvider class to provide access to the data and must be declare in  AndroidManifest.xml.           
  
Content providers encapsulate data and provide it to applications through the single ContentResolver interface. The client use indirectly the ContentResolver object.You get a ContentResolver by calling getContentResolver() from within the implementation of an Activity or other application component. Any content provider is invoked by a URi in the form of content://provider_name . for example the URi of the Contacts content provider that retrieves all contacts is in the following form content://contacts/people. If you want to retrieve a particular contact (by its ID) then it would be in this form: content://contacts/people/5.

You do not need to write the URis of the content providers manually as they are stored as constant values in their respective content provider classes.

The Uri of the Contacts phones content provider is defined in 


          
 ContactsContract.CommonDataKinds.Phone.CONTENT_URI     (content://com.android.contacts/data/phones)

The Uri of the browser Bookmarks content provider is defined in 

 Browser.BOOKMARKS_URI
 (content://browser/bookmarks)

The Media store (Video) stored in external device (SD Card) is defined in 

        MediaStore.Video.Media.EXTERNAL_CONTENT_URI
          (content://media/external/video/media) and so on.

Content providers allow you to perform basic CRUD operations: Create,Read, Update and Delete on data. To retrieve data from a content provider we run a sql-like query using ManagedQuery object. The ManagedQuery object returns a cursor holding the result set.

Remember to add the permission to the manifest.xml file

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

Inserting,updating and deleting:  to insert data using a content provider there are two methods

First: Using the Insert() method of your activity’s content resolver object. Like this example insert a new bookmark to the browser:
ContentValues cv=new ContentValues(); 
cv.put(Browser.BookmarkColumns.TITLE, "End Gadget"); 
cv.put(Browser.BookmarkColumns.URL, "http://www.engadget.com/"); 
cv.put(Browser.BookmarkColumns.BOOKMARK,1); 
Uri u= getContentResolver().insert(Browser.BOOKMARKS_URI, cv);
We create a ContentValues object and add to it all the required fields, then we call getContentResolver().insert method which returns the Uri of the newly inserted item. It would be in this example content://browser/bookmarks/17  We can use the generated Uri to update or delete the item later.

Second: We can replace the getcontentresolver().insert() method with bulkInsert method if we want to insert multiple items at a time. The bulkInsert(Uri url,ContentValues[] values) method returns the number of new items created.

Updating info using Content Providers: To update data using content providers, we use getContnetResolver.Update() method: This code updates the title of an existing browser bookmark:


ContentValues cv=new ContentValues(); 
cv.put(Browser.BookmarkColumns.TITLE, "End Gadget mod"); 
//uriBook= getContentResolver().insert(Browser.BOOKMARKS_URI, cv); 
getContentResolver().update(Browser.BOOKMARKS_URI, cv, BookmarkColumns.TITLE+"=?", new String[]{"End Gadget"})
the Update method has the following parameters:
  • Content Providers Uri
  • ContentValues object having the new values
  • Where clause
  • String array of the where clause arguments value
Deleting info using Content Providers: To delete info we use getContentResolver.Delete() method. To delete a bookmark:
getContentResolver().delete(Browser.BOOKMARKS_URI,BookmarkColumns.TITLE+"=?", newString[]{"End Gadget"});
the delete function has the following parameters:
  • Content Providers Uri
  • Where clause
  • String array of the where clause arguments values
Remember to add the following permissions to the manifest.xml to add and read browser bookmarks:
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"></uses-permission>

No comments:

Post a Comment