Friday 10 May 2019

Content Provider, Content Resolver, Content URI


  • What are Content Providers and why using them?
    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 files, in a database or even over a network.
A content provider coordinates access to the data storage layer in your application for a number of different APIs and components as illustrated in figure 1, these include:
  • Sharing access to your application data with other applications
  • Sending data to a widget
  • Returning custom search suggestions for your application through the search framework using SearchRecentSuggestionsProvider
  • Synchronizing application data with your server using an implementation of AbstractThreadedSyncAdapter
  • Loading data in your UI using a CursorLoader

Content Providers support the four basic operations, normally called CRUD-operations. CRUD is the acronym for create, readupdate and delete. With content providers those objects simply represent data as most often a record of a databasebut they could also be a photo on your SD-card or a video on the web.


  • Reasons to use a content providers:
    (1) Extra level of abstraction
    Easily change underlying data source
    (2) Leverage functionality of Android classes (ex: Loaders)
    (3) Allow many apps to access, use and modify a single data source securely.

    If we wouldn’t use a content provider our app data will be accessible only for our app.

    All data requests should go through the content provider class.

    To work with Content Provider we need to do the following steps:
    (1) Get permission to use the ContentProvider.
    (2) Get the ContentResolver
    (3) Pick one of four basic actions on the data: query, insert, update, delete
    (4) Identify the data you are reading or manipulating to create a URI
    (5) In the case of reading from the ContentProvider, display the information in the UI

    The loader API lets you load data from a Content Provider or other data source for display in an activity or fragment.
  • Content Permission
    In the Android Manifest we should ask for a read/write permission to use the content provider. it’s a security feature of content providers that they’re protecting by the same permission system that will popup user dialogues, which inform the user of what the app actually does.
  • Content Resolver and the four basic actions
    The Content Resolver is a single global instance in our app that provides access to our and other applications Content Providers. The Content Resolver is a class which sits between our app and the Content Provider.

    The Content Resolver acts as intermediary between each app and the content provider or providers it wants to access. It handles inter process communication and keeps everything in sync and running smoothly.

    Whenever we want to use a Content Provider, we’ll need to do it by using a Content Resolver.

    In the code we first will get a reference to the system’s Content Resolver.

    Next, we need to use the Content Resolver to call the method query. The query method is part of the set of 4, mapping to the 4 CRUD methods.

    There are 4 basics things you can do with a Content Provider:
    (1) Read from the data
    query()
    (2) Add new row(s) to the data
    insert()
    (3) Update the data
    update()
    (4) Delete row(s) from the data
    delete()

    We specify what we want to do by calling ContentResolver. And then one of those methods. Then the Content Resolver tells our Content Provider to perform that action.

    Activity/Fragment <-> Cursor Loader <-> Content Reslover <-> Content Provider <-> Data Source
  • URIUniform Resource Identifier
    URI is used to specifically identify or give the location of some data on your phone. URI defined the exact data you’re trying to read or manipulate.

    This location is how you know exactly what type of data we’re querying for. The location is build from 3 parts:
    (1) content://
    The content provider prefix
    (2) The content authority
    specifies which Content Provider to use
    (3) Specific data
    a string that identifies exactly what data in the Content Provider we’re interesting in accessing.

    The structure of this string knows as the path, will be specific to the content provider.

    URLs are a subset of URIs that are specifically met to identify network locations such as websites and files on the web.
  • How to call a Content Provider and How to use a Cursor
    Database operations are one of the things that can take a really long time. That’s why all db operations should NOT be on the main thread, a.k.a, always use a background thread.

    One option to implement a calling to a Content Provider is to use an AsyncTask that will return a Cursor. We need this cursor to be available in the main activity of our app.

    The query method returns a cursor which is exactly the same object returned by Android SQLite database class. Cursors are iterators that provide read/write access to the data of a Content Provider.

    The data is in a tabular format. It can be costly to get all the information, that’s why we have some parameters in the query() method that we can use:
    (1) Projection
    Filtering columns
    (2) Selection
    Statement for how to filter rows
    (3) Selection Arguments
    What to filter
    (4) Sort Order
    How to sort rows

    How do we work with cursors?
    The data of a cursor is tabular. A cursor has a position, which is the row it’s currently pointing to. When you first get your cursor back from the query() method, it positioned in row (-1), which is nothing. There are a few methods we can use to change the cursor’s position:
    (1) moveToNext()
    Moves to next row | Returns TRUE or FALSE
    (2) moveToFirst()
    Moves to first row
    (3) getColumnIndex(String heading)
    Given a heading of a column, returns index
    (4) get<Type>(int ColumnIndex)
    Returns the value at the column index. <Type> can be String, int, long, etc.
    (5) getCount()
    Returns the number of rows in cursor
    (6) close()
    Always close your cursor to prevent memory leaks


https://developer.android.com/guide/topics/providers/content-providers

https://medium.com/@sanjeevy133/an-idiots-guide-to-android-content-providers-part-1-970cba5d7b42

https://medium.com/@paulnunezm/steps-for-creating-a-content-provider-ab376d661613

https://medium.com/@saranyaan2710/content-provider-in-android-basics-d219cf42574d

https://medium.com/@gadi.krn/android-content-providers-get-your-data-e80a233e131

https://medium.com/@gadi.krn/android-content-providers-build-your-own-586e1869eece

No comments:

Post a Comment