In Android , Content Providers are a very important component that serves the purpose of a relational database to store the data of applications. The role of the content provider in the android system is like a central repository in which data of the applications are stored, and it facilitates other applications to securely access and modifies that data based on the user requirements. Android system allows the content provider to store the application data in several ways. Users can manage to store the application data like images, audio, videos, and personal contact information by storing them in SQLite Database , in files , or even on a network . In order to share the data, content providers have certain permissions that are used to grant or restrict the rights to other applications to interfere with the data.
Content URI(Uniform Resource Identifier) is the key concept of Content providers. To access the data from a content provider, URI is used as a query string.
Structure of a Content URI: content://authority/optionalPath/optionalID
Details of different parts of Content URI:
If an ID is mentioned in a URI then it is an id-based URI otherwise a directory-based URI.
For a deeper understanding of how to implement Content Providers and other critical Android components using Kotlin, consider enrolling in the Android Development with Kotlin course . This course is designed to equip you with the skills necessary to build advanced Android applications, leveraging Kotlin’s features to enhance your development process.
Four fundamental operations are possible in Content Provider namely Create , Read , Update , and Delete . These operations are often termed as CRUD operations .
UI components of android applications like Activity and Fragments use an object CursorLoader to send query requests to ContentResolver. The ContentResolver object sends requests (like create, read, update, and delete) to the ContentProvider as a client. After receiving a request, ContentProvider process it and returns the desired result. Below is a diagram to represent these processes in pictorial form.
Following are the steps which are essential to follow in order to create a Content Provider:
Following are the six abstract methods and their description which are essential to override as the part of ContenProvider class:
A method that accepts arguments and fetches the data from the
desired table. Data is retired as a cursor object.
To insert a new row in the database of the content provider.
It returns the content URI of the inserted row.
This method is used to update the fields of an existing row.
It returns the number of rows updated.
This method is used to delete the existing rows.
It returns the number of rows deleted.
This method returns the Multipurpose Internet Mail Extension(MIME)
type of data to the given Content URI.
As the content provider is created, the android system calls
this method immediately to initialise the provider.
The prime purpose of a content provider is to serve as a central repository of data where users can store and can fetch the data. The access of this repository is given to other applications also but in a safe manner in order to serve the different requirements of the user. The following are the steps involved in implementing a content provider. In this content provider, the user can store the name of persons and can fetch the stored data. Moreover, another application can also access the stored data and can display the data.
Note: Following steps are performed on Android Studio version 4.0
Creating a Content Provider:
Step 1: Create a new project
Step 2: Modify the strings.xml file
All the strings used in the activity are stored here.
User Name Provider In Android Data DataStep 3: Creating the Content Provider class
This class extends the ContentProvider base class and override the six abstract methods. Below is the complete code to define a content provider.
Kotlin
Step 4: Design the activity_main.xml layout
One Textview , EditText field, two Buttons , and a Textview to display the stored data will be added in the activity using the below code.
Step 5: Modify the MainActivity file
Button functionalities will be defined in this file. Moreover, the query to be performed while inserting and fetching the data is mentioned here. Below is the complete code.
Kotlin
Step 6: Modify the AndroidManifest file
The AndroidManifest file must contain the content provider name, authorities, and permissions which enable the content provider to be accessed by other applications.
Creating another application to access the Content Provider:
Step 1: Create a new Project
Step 2: Modify strings.xml file
All the strings used in the activity are stored in this file.
data of Content Provider DataStep 3: Designing the activity_main.xml layout
Two TextView are added in the activity, one for heading and one to display the stored data in a content provider. One Button is also added to receive the command to display data. Below is the code to implement this design.
Step 4: Modify the MainActivity file
The ContentURI of the previous application is mentioned here and the same functions which were used in the previous app to display the records will also be used here. Below is the complete code:
Kotlin
Step 5: Modify the AndroidManifest.xml file
Don’t forget to add permission (API 30 and above).
For more details refer here .