Android Content Provider - Androhub

Content Provider

Android Content Provider

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.

Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content using insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database.

Content Provider

A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.

Content URIs

To query a content provider, you specify the query string in the form of a URI which has following format:

Here is the detail of various parts of the URI –

PartDescription
prefixThis is always set to content://
authorityThis specifies the name of the content provider, for example contacts, browser etc. For third-party content providers, this could be the fully qualified name, such as com.androhub.statusprovider
data_typeThis indicates the type of data that this particular provider provides. For example, if you are getting all the contacts from the Contacts content provider, then the data path would be people and URI would look like this content://contacts/people
idThis specifies the specific record requested. For example, if you are looking for contact number 5 in the Contacts content provider then URI would look like this content://contacts/people/5.

Create Content Provider

This involves number of simple steps to create your own content provider.

  • First of all you need to create a Content Provider class that extends the ContentProvider base class.
  • Second, you need to define your content provider URI address which will be used to access the content.
  • Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework needs to override onCreate() method which will use SQLite Open Helper method to create or open the provider’s database. When your application is launched, the onCreate() handler of each of its Content Providers is called on the main application thread.
  • Next you will have to implement Content Provider queries to perform different database specific operations.
  • Finally register your Content Provider in your activity file using <provider> tag.

Here is the list of methods which you need to override in Content Provider class to have your Content Provider working:

  • onCreate() – This method is called when the provider is started.
  • query() – This method receives a request from a client. The result is returned as a Cursor object.
  • insert() – This method inserts a new record into the content provider.
  • delete() – This method deletes an existing record from the content provider.
  • update() – This method updates an existing record from the content provider.
  • getType() – This method returns the MIME type of the data at the given URI.

In last tutorial we had covered the topic how to fetch the contact details from phone contacts using content provider i.e. inbuilt content provider. In this tutorial we are going to create our custom content provider.

Example

In this tutorial, we are going to learn how to create custom content provider and how to fetch the data from this content provider from another app.

Video Demo

Custom Content Provider Example

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial.

3. Now, create a new java class naming Custom_ContentProvider.java extending ContentProvider. In this java class we put all queries like insert, update, delete and query. We also created a sqlite database to store the data.

If you are unaware of sqlite database you can check the tutorial here.

4. After creating custom content provider we have to put it into manifest file inside provider tag.

5. Now create a basic xml layout naming activity_main.xml. In this layout i put some edittext and two buttons.

6. Finally come to MainActivity.java and add the following code to it. In this java class we have two onClickListener buttons :

  • Add Button – This button will add the EditText values into Content Provider database using Content Values.
  • Show Button – This button fetch the data from content provider in cursor and check if cursor is not null then show data else show empty data.

7. Finally you are done with custom content provider example.

Use of Custom Content Proivder Example

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial.

3. Create a xml layout naming activity_main.xml. This layout contains some buttons for showing data, inserting new data, update data and delete data.

4. Now, create a new class naming Random_Datas.java that contains three string array for adding new data inside content provider.

5. Finally come to your MainActivity.java and follow some steps while fetching data from custom provider app.

  • We have to use the URI that we have created in our Custom Content Provider.

  • Before fetching data, inserting data, updating data or doing anything with content provider first check if content provider app is installed in your device and containing some data to perform task else exception will occurs.
  • On Content Provider data update and delete i am using id to do this task you can use any field like name, email, etc. Make sure before updating first check if particular id is present in database or not.

Final source code of MainActivity.java.

6. Finally you are done with this example also.

Now you can use both example and run the app and you will get the output as shown in the video.

Thanks. :)

 

1 Comment

james
Sunday, October 8th, 2023

i like your beard – git hub prof pic

Post comment

Your email address will not be published. Required fields are marked *