Introduction
Content provider helps to share data between application in a proper and secure manner. Content provider connects any other application by implicit intent and parses the Uri of that provider app. Then a cursor is assigned to each record in the target application that exports data one by one through the database to our application.
Example: Here we have an example of whatsapp wherein the first step we have the view where we set out name and contact number from contact. In the second step, an implicit intent is fired to take a connection through URI which parses to the app. So actually here phone app doesn't have data of contact and it is in the database. So a cursor is assigned and through content resolver, it takes the data one by one from the storage and export or sets it to our application.
![]()
Now let's dig into the practical aspect and see the actual implementation of content provider by setting a phone contact from contact app on our app screen.
Step 1 - Create a new project in Android Studio Application
Here I have created a blank application named Content Provider
![]()
Step 2 - Application Layout or .xml file
Here we will add two Edit Text and a Button for displaying our Contact Data. The details of Activity_main.xml file are as follows -
~ 2 TextView --> Name & Contact number
~ 1 Button --> Button(or you can set your choice)
Step 3 - Granting Permissions or AndroidManifest.xml file
You need to get permission from Android OS to read Contacts data from storage. You can do this by adding <user-permission> in AndroidMenifest.xml File
~ Add <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> like this
Step 4 - Dealing with MainActivity.java File
We have to follow these steps to make the below code
1. Initialize Button -->
2. Under Oncreate method -->
3. Implement View.OnClickListener on Mainactivity then under sideline number click on warning and click on Create OnClick method then a OnClick method will be initialized
4. Under OnClick method -->
5. Create OnActivityResult method just by typing and pressing tab and follow the below code
Full code snippet is as follows.
Output
Here is our application and when we click to submit we will move to contact and select desirable contact.
![]()
Then we will display the following output
![]()
Conclusion
In this article, we have learned about Content provider and made a successful app to display the contact details through Contact app on our mobile.