By using the content provider, data can be shared across applications. The content provider is a set of data wrapped up in a custom API to read and write. Applications/Processes have to register themselves as a provider of data. Other applications can request Android to read/write that data through a fixed API.
Content provider API has methods to perform operations(CRUD) on data.
Contacts are the best example – that exposes user information to other applications, Media store-Allows other applications to access, store media files.
There is some standard methods – insert(), query(), update(), delete(), to access application data. So it is easy to implement a content provider.
We need to assign a URI to each Content Provider, starting with “content://” and that will be recognized by applications.
1. Create a class for ContentProvider.
2. Define content URI
3. Implement all the unimplemented methods. insert(), update(), query(), delete(), getType().
4. Declare the content provider in AndroidManifest.xml
1. content:// – The content provider URIs should start with this value.
2. ‘authority’ – This is Java namespace of the content provider implementation passes the Java package name.
3. ‘path’ – This is a virtual directory within the provider that identifies, what kind of data being requested.
4. ‘id’ – This is an optional part that specifies the primary key of a record being requested. This part can be ignored to access all data.
We need to override insert() method. If data inserted successful, it will return the URI value with the associated with ID.
For example: If we passed – content://packageName/sample
Method will return content://packageName/sample/1
update() method of the ContentProvider is used to update records.
This method will return the number of rows updated.
delete() method of the ContentProvider will return the number of records deleted.
We need to register the content providers in the AndroidManifest.xml.
Be the first to comment.