Pages

Friday 13 September 2019

How to use content provider in android

When to use content provider:
  • When we want to share our data, files etc to other application then we can use content provider.
    Following diagram illustrate basic of it.
    Overview diagram of how content providers manage access to storage.
  • Let's consider content provider app as server app and the app who use it called client app.
    So to use server app's data, client app needs to use ContentResolver class to perform query to the server app. server app will return cursor to client app so client can iterate through it and read data from cursor. Following diagram illustrate it
    Interaction between ContentProvider, other classes, and storage.
  • Lets start server side (ContentProvider related) Example.
    Consider following package and files structure in server app already there so that we can only focus on content provider. It mainly having Database related files using Room library. Following is structure with use of each file
    • content_provider_demo (package)
      • dao (package)
        • RecentSearchDao : Dao class to perform query on recent search table
      • ContentProviderDemoActivity: demo activity to get result from content provider and print in logs.
      • Database: Singleton class to create RoomDatabase object if not exist
      • Detail : contains static variables. i.e. Db name etc
      • RecentSearch: Table of recent search, made using Room lib
      • RecentSearchProvider: Our provider class. we will discuss about it in next section
      • ScoscheDatabase: Room database config class contains all Dao and Entity.

  • We are going to provide access of our "Recent search" data to other app using "RecentSearchProvider" class. To do that first we have to create "RecentSearchProvider" class as following. I have just use insert and query method for demo purpose. you can use other methods like update, delete etc How it works : - User can use this content provider using content resolver. in content resolver we need to pass unique URI so that system can find which provider need to use and what table and data need to access.  - We can construct Uri like following.
    Uri = host://authority/table
    Here, host is fixed string "content://".
    authority is any unique string which we need to define. We can construct it like following.
    authority = package_name + "."+ your provider class name
    - By URI passed in ContentResolver system can find our class and the method (query, insert etc) need to be call. Now user can do different select queries. For ex user can select all recent searches or user might need recent search for given id etc etc.  so that we have created URI matcher here, So to get all recent search user need to use this content URI "content://$PROVIDER_NAME/$tblRecentSearch". To get recent search for ID = 4 user may use this type of provider "content://$PROVIDER_NAME/$tblRecentSearch/4".
  • We have to register this content provider class in AndroidMenifest.xml file to allow its access to apps. We can do it by adding following code in "application" tab.  Here, "name" contains path of our Content provider class and "authority" contains the authority of this provider.
  • So server side (or ContentProvider related) logic done here.
  • Client side (ContentResolver related) part start here. Second part is "How to access data from ContentProvider". We need to create ContentResolver object to access data from ContentProvider. Android have getContentResolver() (contentResolver property for Kotlin) method to create object of ContentResolver. ContentResolver have different methods like "insert","query","update", "delete" etc to access data. We need to use ContentUri defined previously in content provider to access its data. To insert data to recent search we can do like following.


  • To get records from content provider we can use "query" method of contentResolver. It will return cursor object so we can iterate through it to fetch data. Following example fetch all recent searches.


  • That's it for contentResolver. Second part of code can be found in "ContentProviderDemoActivity". Client side (ContentResolver related) ends here.
  • You can find whole demo in Github. Github Link: https://github.com/k9428/Content-Provider-Demo

1 comment :

  1. thank You very Much:)) I was looking for just like this, real kotlin solution for content provider:) thanks!!

    ReplyDelete