What is difference between contentprovider and contentResolver in android

前端 未结 4 1869
野趣味
野趣味 2021-01-30 13:09

What is the difference between ContentProviders and ContentResolver? I do not want for the SQLite database. I am developing an app

4条回答
  •  误落风尘
    2021-01-30 13:17

    Two layered Abstraction :

    ContentResolver --> ContentProvider -->SQLiteDatabase

    The main difference is this as mentioned in other answers.

    ContentProvider exposes private data of your application to external application
    while
    ContentResolver provides the right ContentProvider among all the ContentProviders using a URI.

    Deeper Understanding (of two-layered abstraction)

    Let's take a detour.
    We all know that when we create an SQLite database then the database remains private to your application which means, you just can not share your app data with any other external application.

    How data is shared then?

    ContentProvider and ContentResolver are part of android.content package. These two classes work together to provide robust, secure data sharing model among applications.
    ContentProvider exposes data stored in the SQLite database to other application without telling them the underlying implementation of your database.
    So it abstracts the SQliteDatabase. But wait there is a catch !!!
    The external application can not directly access ContentProvider. For that, you need to first interact with another class called ContentResolver Think ContentResolver as a ContentProvider finder. There is only one instance of it and all the ContentProviders of your Device are registered with a simple Namespace URI. If you want to reach a particular ContentProvider you just need to know its URI. Pass it to ContentResolver and it will find the Provider using the URI.
    Now lets have a look at the most important method getContentResolver().query(URI,String[] proj.....)

    What happens when getContentResolver().query(URI,String[] proj.....) gets called

    query() method belongs to ContentResolver class however it invokes the abstract query() method of resolved ContentProvider and returns Cursor object.
    In this way, the External application gets exposed to the private database via two abstraction layers.

    Just to add more points
    You cannot create your own ContentResolver class but you can always create your own ContentProvider class

    Hope you have a better understanding
    You can also see some sample code here for creating SQLitedatabase, ContentProvider etc, But it's not well documented.

提交回复
热议问题