Content Providers - Consuming data from a Content Provider using Content Resolver
Content Providers are used to share data between applications
When is it necessary to implement Content Provider?
- When we have to expose our apps data with other app
- Need some complex data copy functionality from our app
- We intend to use :
Cursor Loader
Cursor Adapter
AbstractThreadedSyncAdapter
Data from Database
FIle Data(JSON, XML, Audio, Video, Photos)
Networked Data - AbstractThreadedSyncAdapter
How to implement Content Provider?
We need to make sure we have primary keys defined in our tables else Content Providers will not work properly
Implement Custom ContentProvider class
Define proper Content URIs for data to be exposed
Content URI?
Is an important concept to access any data from a ContentProvider.
Assuming we are sharing data from database :
Content URI has 3 components :
Content : content://
Authority : Package Name of the App
Path : User Defined String Constants
Suppose App2 needs some data from App1
App2 will use an API called ContentResolver(Data Request) using which we will hit App1 database and the content provider defined in App1 will respond back using the format called Cursor(Data Resposne).
Cursor is an API which allows us to traverse through those rows defined in that particular tabular format data. Its a reference which we want to iterate over a table with multiple no of rows.
It is Inter Process Communication(IPC). What content provider does is it provides a kind of abstraction from handling this IPC.
Eg : In real world scenario, we have the Contacts App(APP1) and WhatsApp(APP2). It doesn't matter how the data is stored in Contacts App, it may be in JSON file, DB, File or XML. As long as content provider exposes that data, APP2 should be able to parse through the data being returned from the content provider defined in Contacts App.
The Context object contains a method called getContentResolver() which returns the instance of a Content Resolver. To hit the correct Content Provider, we should know the Uniform Resource Identifier(URI). URI is something like a URL which we use to hit a website.
URI will be unique to that Content Provider. The Content Resolver provides us certain methods using which we can perform CRUD & Batch operations.
Usual SQL Statement :
SELECT col1, col2 FROM table_name WHERE col1 = value ORDER BY col2
Equivalent Query Method :
getContentResolver().query(<ContentProvider>.Content_Uri, mProjection, mSelectionClause, mSelectionArgs, mSortOrder)
<ContentProvider>.CONTENT_URI : Most of the content providers are defined under android.provider package.
mProjection : Nothing but the number of columns that we want to query from table which is nothing but the string array representing the names of column which we need to query from content provider
mSelectionClause : Equivalent to WHERE clause. Its a String datatype.
mSelectionArgs : If we want to pass any arguments during runtime
mSortOrder : The order in which the query need to be sorted
Comments
Post a Comment