Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The title sums up my question, but to elaborate basically what I want to understand is why the Android designers want apps that need to work with shared data to use a Content Provider rather than just accessing the SQLite database directly?

The only reason I can think of is security because certain files can by accessed only be certain processes and in that way the Content Provider is the gatekeeper that ensures each app has the proper privileges before allowing read and/or write access to the database file. Is that the primary reason why ContentProvider was created?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

It is above all a way of insulating data consumers and data providers. You develop your own content provider or extend an existing one if you want to make some of your data public or at least available to other application.

True this can server to control accesses from a security point of view but it also allows you to rework the physical implementation of your data whenever you want. All you need to do is to adapt the back-end of your content provider in that case. The data consumer applications will not have to be rewritten. They will carry on accessing your data through their content resolver unaware of any change in the actual underlying implementation.

Also, Android will instantiate only one instance of your content provider even if the data is accessed by several clients so it will take care of concurrent accesses without you having to care about it.

Finally, I believe it will also handle the drudgery of clean start up and shut down.

share|improve this answer
1  
I think the insulation is probably the best answer. I don't agree with your point about concurrent access, SQLite and the Android Java interface handle the concurrency, ContentProvider itself allows multiple simultaneous threads to query/insert/update so it's not really doing anything for you. –  satur9nine Jun 9 '11 at 22:21

The ContentProvider also abstracts out all of the inter-process communication that is required in order to communicate with other third-party applications. Having to write this code yourself would be a huge pain.

share|improve this answer
    
3rd parties could always just access the database file directly, no inter-process communication needed there. –  satur9nine Oct 15 '12 at 16:31
    
I'm not sure what you mean... if an app is a "third-party" then by definition it must exist in a different process (since every Android app has it's own main process). Also if the Android OS allowed you to have direct access to other application's raw data stores, that would raise some pretty big security issues. –  Alex Lockwood Oct 15 '12 at 17:17

ContentProvider is also an abstraction that hides the details of how the data is stored/generated. For example, in one of my apps I have a content provider that returns PNG images. These images aren't stored anywhere, they are generated on demand.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.