If you really want to offer structured data or files to other applications, then you’ll have to write your own custom content provider. The entire process of creating your own content provider has various steps involved:
- Create a contract class with all the constants defining URIs, tables, columns, etc. Here’s a nice example of the UserDictionary contract class that defines various constants for content URI, authority, etc. More information on contract class can be found here and here.
- Create an
SQLiteOpenHelper
implementation that creates/upgrades your database and can be used to access the database from your content provider. - Finally, create your
ContentProvider
implementation by writing a class that extendsContentProvider
. This class will need to implement several methods likequery()
,insert()
,update()
,delete()
,getType()
,onCreate()
. ThegetType()
method should return the MIME type corresponding to a content URI (more information here). TheonCreate()
method is called right after the provider is created which happens when aContentResolver
object tries to access it. This method can be used to instantiate yourSQLiteOpenHelper
implementation and save a reference to it (instance variable). All the other methods apart from these two are basic CRUD methods. - Add the content provider to the manifest file.
Although I was about to explain the entire process, but then I realized that the documentation does a good job (easy to understand) and is pretty comprehensive too. So here are a few links that you should definitely read to completely get a hang of how to create your own custom content provider:
What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.
- https://developer.android.com/guide/topics/providers/content-provider-creating.html
- http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
- The thread safety block here is a very important piece of information to keep in mind.
Hope that helps!
Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download