何时使用内容提供程序

据我所知,Content Provider 允许在应用程序之间公开共享数据。然而,我想知道是否有人想过创建一个内容提供者,只在你自己的应用程序中使用。这样做有什么好处吗?有什么缺点吗?

在过去,我只是实现了 SQliteOpenHelper 来访问数据库中的数据,但是我正在考虑创建一个 Content Provider。我觉得请求数据的 URI 方法是清晰和简洁的。另一方面,仅仅为我的应用程序使用 Content Provider 是多余的(因为在其中我将有一个 SQliteOpenHelper 类)并且比我需要的工作更多吗?

32977 次浏览

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.

Take a look at the MOTODEV Studio for Eclipse. It is a development environment that extends Eclipse. They have a tool where you can automatically generate a content provider for a database. If a content provider makes it easier to access your data and it doesn't have a significant impact on performance go ahead and use it. In most scenarios this will be the case.

I would argue it is definitely a good idea to use a ContentProvider even if you don't intend to make it public.

It's good practice to provide the extra level of abstraction over your data to make it easier to change internally. What if you decide to change the underlying database structure at a later time? If you use a ContentProvider you can contain all the structural changes within it, where as if you don't use one, you are forced to change all areas of the code that are affected by the structural changes. Besides, it's nice to be able to re-use the same standard API for accessing data rather than littering your code with low-level access to the database.

Also, there is always the chance that you might want to expose your data in the future. If you don't use a ContentProvider up front, it will be much harder to retrofit it in at a later date.

Then, there's the other parts of the Android where ContentProvider's are required/recommended such as when using SyncAdapters and if you want an App Widget that involves data access for instance.

In summary, there is very little overhead involved in writing a ContentProvider up front (once you have learned the API which is a good idea anyway) so it makes sense to do so, even for private data.

I agree ContentProviders are a little difficult to grasp but they are definitely helpful, even if you want to use them internally for you own app. The best thing about it is that you can customize the contentproviders for suitable URIs.

Here's a scenario where you may have 5 tables in your database, but you need to join a few of them in certain orders before using them. And make a content URI for each of these joins. You could then each use these URIs as a table :)

I suggest you go ahead with Content Provider, you'll be amazed to see how powerful it is.

Do not use content provider if do not wish to share data with other apps. Use simple sqlitedatabase to perform database operations. Be careful while using content providers for storing confidential data because your confidential information may be accessed by other apps

In my view point, the content-provider comes with plenty of advantages leave alone just sharing data with other apps. If you need to synchronize with the server using a Sync-Adapter, use google cloud messaging, auto update the UI when the underlying data in the DB changes using Loaders, implement search, use widgets... then the content provider is for you.

I prefer you follow the guideline on because one day you may need to implement some of the above features attached to the content-provider

By the way, you can quickly build you database and CP in less than 5 minutes using content provider generator

As said in documentation: Creating a Content provider

You don't need a provider to use an SQLite database if the use is entirely within your own application.

So why bother developing this overhead? You want easier and faster development, right? So one layer of abstraction (SQLiteOpenHelper descendent) is enough.

See Occam's Razor Do not make an entities without very good reason.

In short,Content Providers helps in managing your data effectively. I would suggest to use them for the following reasons.

  • It acts as an abstraction layer between your UI and database. You can implement data validation in ContentProviders to validate the data entered by the user. It also lets you to modify the structure of the database without touching the UI and other parts.
  • They play along nicely with other android framework classes like SyncAdapter. For eg., you can automatically refresh a list, when a value in a database changes using ContentProviders along with CursorLoader. Without ContentProviders you have to implement a lot of functionalities like these on your own.
  • We can safely expose our private data to other apps. Using ContentProviders will allow us to share our data easily and safely with other apps.

So even if you don't need any of these functionalities now, you might need them in future and its good to go the extra mile and implement them right now.

Using a Content Provider can help in an additional level of abstraction - Putting it within your own application make add a significant development time to your project. However if you are using it to share data, application settings or configurations across multiple applications then the Content Provider is your choice.

Watch your security levels and I would recommend using SQLcipher to encrypt data-at-reset (DAR) if your Content Provider is writing to SQLite. (I've used a content provider in a few solutions and provided the ability to take a live "snap shot" of the operational values for debugging and testing.)