Content Providers ContentResolver with an example in Android

Content Providers is the main component of Android app development. The first introduction was for share data with other app or within the application. But now can store and secure your data during uses. Content providers have an interface which connects data in one process with code running in another process. Where ContentResolver resolves a URI to a specific Content Provider.

Content Providers ContentResolver example tutorial studio

If you want to share data or donate want to, you may use a content provider because it provides a great abstraction. This abstraction allows you to make modifications on your application data storage implementation without affecting other existing applications that rely on access to your data. In this whole scenario, only your content provider is affected and not the applications accessing it.

Content providers can use for structured data like SQLite relation database and unstructured data such as Images, Video and Audio files. Its manage access to a variety of data storage source.

 

Content Providers control over the permissions

  • Restrict Access –  Only within your application
  • Grant blanket permission – Can access data from other applications
  • Configure different permissions for reading and writing data

NoteCursorLoader objects depend on content providers to run asynchronous queries that return the results to the UI layer in the application.

 

Before start just get in What is ContentResolver :

For access data in a content provider, you have to use the ContentResolver object in your application’s Context to communicate with the provider as a client.

TheContentResolver methods provide the basic “CRUD” (create, retrieve, update, and delete) functions of persistent storage.

 

Let’s build an App for Content Providers 

You can code to access an existing content provider in another application, or can create a new content provider in your application to share data with other Android applications.

This example tutorial is to cover the basics of working with existing content providers.

Content Providers ContentResolver example tutorial studio database

In this example, we build an app for contact list (like a phone directory). Where all contact will store in database SQLite and access them we use ContentProvider and ContentResolver.

 

CursorAdapter: Adapter that exposes data from a Cursor to a ListView widget.

CursorLoader: Run asynchronous queries that return the results to the UI layer in the application.

Content URIs<prefix>://<authority>/<data_type>/<id>

# Description
prefix content:// standard prefix indicates data controlled by Content Providers
authority Name content provider Like contactsbrowser, etc. For third-party content providers. 

It could be a fully qualified name, such as in.eyehunt.mycontactcontentproviders

data_type Type of data that this particular provider provides.
id Specific record request. For example, search for contact number 3 in the Contacts.
Step 1. Create a new project “ Build Your First Android App in Kotlin 

 

Step 2. Start With you add floating action button and ListView in “activity_main.xml

Before add icons in a mipmap resource file and outline_delete.png & baseline_add_white.png

Add Strings res in project

Now come into main_activity.xml

Step 3. Create a resource file “row_contact.xml” to show the item as an in List

Step 4. Create a dialog box resource file “dialog_box.xml

Step 5. Create a menu “main_,menu.xml”

if you don’t have any menu resource file then create first “Android Resource directory” then create resource file “main_menu.xml”

Step 6. Create a class DatabaseHandler extends SQLiteOpenHelper

Create a table and database to store contacts

Step 7. Create a MyContactsProvider class extends ContentProvider

The override must methods and add this code to perform the operation in DB thru Content Providers

Step 8. Create ContactsCursorAdapter  adapter extends CursorAdapter

Step 8. Add the following code in MainActivity.java class

Step 9. Run the application, in the emulator or On your Android device
Output screenshot Content Providers ContentResolver example in Android  :

Content Providers ContentResolver example output

 

Video Output

Download source code Android Content Providers ContentResolver example

https://github.com/EyeHunts/MyContactContentProviders

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Java


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.