When to Use a Content Provider

When to use a Content Provider

If you are not planning to share data, don't think about Content Providers. They are powerful but hard to write and it will be just silly to implement them if you are going to use them internally.

However, I'm wondering if anyone has thoughts about making a Content Provider to use just within your own app.

Of course... for instance, for an old TODO list app I wrote, I had to write a content provider to allow other apps retrieve and access the tasks states. It was part of the requirements, but more than that it made sense and made the app nicer.

What is a ContentProvider and what is it typically used for?

A ContentProvider manages access to a structured set of data. It encapsulates the data and provide mechanisms for defining data security. ContentProvider is the standard interface that connects data in one process with code running in another process.

Sample Image

Kindly refer following links,

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

and

https://www.tutorialspoint.com/android/android_content_providers.htm

Should I use a Content Provider?

Personally, I have been using ContentProviders in all my projects for the last year and a half. They provide good, database independent, data abstraction to access your data. They are very flexible, I even had a play project where one URI pointed to a SharedPreference while all others where for accessing database tables. ContentProviders also allow you to use already built framework infrastructure such as CursorLoaders, for example. Implementing your own from interfaces and abstract classes is not that hard, but it may be time consuming and error prone, being able to just leverage work that's already been tried and tested is a great advantage.

By the way, I remember the same exact question on a post in google+ about 2 weeks ago where Cyril Mottier gave a very good answer. You can read it here.

Exact Difference between Content-Provider and SQLite Database

I found one major difference, as follows:

Storing your data in a database is one good way to persist your data, but there's a caveat in Android-databases created in Android are visible only to the application that created them. That is to say, a SQLite database created on Android by one application is usable only by that application, not by other applications.

So, if you need to share data between applications, you need to use the content provider model as recommended in Android. This article presents the basics of content providers and how you can implement one.

I found this article at this link

Really nice information provided.

Contentprovider: How to use a reference for it?

The comments are on the right track, but I feel that they need elaboration:

Content providers, even ones that you only use within your own app, are not designed to be referenced directly. Instead, an app should use methods in ContentResolver to access a content provider. ContentResolver features the same set of methods as those that you must implement for ContentProvider (its abstract methods). For example, if you want to insert data into a ContentProvider, you call getContentResolver().insert(). A ContentResolver is a general-purpose object for communicating with content providers, file providers, and the sync adapter framework.

When you define a content provider in your app, you have to define its authority value in the manifest, and you also have to write code that interprets incoming content URIs. The best way to work with content URIs is to start by assigning a separate content URI to each table in your provider, using the form

content://*authority*/*path*

Authority is the authority string you specified in the manifest; the de facto standard is to use your app's package name with ".provider" appended to it. Path is a descriptive term for the table it represents. For example:

content://com.example.android.myapp.provider/sales

to point to the Sales table in your provider. You can get fancier if you want, but most developers will satisfy their needs with this proposal.

To work with content URIs in your content provider, use the android.content.UriMatcher class.



Related Topics



Leave a reply



Submit