By Md. Sabuj Sarker | 10/30/2017 | General |Beginners

Understanding Content Providers For Android Programmers

Understanding Content Providers For Android Programmers

Many real life applications need to ask for some data from other applications. For example, WhatsApp, Skype, and Facebook Messenger all ask the Contacts app for the contacts saved in your device. The default messaging application also needs to ask that application for contact details to show who sent the message, and also it needs to communicate with the Contacts application's content provider to select by the names of the contacts to send a message. We should keep our applications DRY (Do Not Repeat Yourself) so that the same data is not replicated throughout the system over and over again. You may even develop several applications of which many may need to get data from some other application from the list of applications you developed. So, should you replicate the same data in all of your application? No, a big NO!

Wait! How do we share data among applications without replicating them? Can data in one application be seen and modified by other applications? No! If that were the case then malicious applications would have stolen all of our personal data and sold them off to the bad guys. The Android operating system does not allow sharing of data without restrictions. It is the application that will decide what data it shares and what is not allowed. It controls the flow of data. It supplies its data to other applications by using Content Providers. To share data with other applications an application needs to have a class that extends the ContentProvider class and implements a few methods for CRUD operations. The content provider also needs to be registered in the manifest file. The applications that want to access data cannot call the methods on the extension class of the ContentProvider directly, if it could it would be responsible for security breaches. To interact with the content provider the consumer application must use Content Resolver available from its contexts.

What type of data can be provided with Content Provider? Any data ranging from plain text to full HD videos. It does not matter in what form the data resides in the host application. The data may live in ordinary files or in some kind of database. In most of the cases we provide data from some SQLite database from the host application. In this article we will concentrate on the provider with an SQLite database backend.

The URI

URL stands for Universal Resource Identifier. In your Android system when you want to identify some record that you want to interact with Content Provider you need to use the URI of certain pattern to individually identify each single piece of data or a set of data.

The format of the URI for content provider is: :////

Prefix: Here prefix is content:// all the time.

Authority: By authority we mean the name of the content provider. For example, contacts, browser, etc. The applications that come with the system might have single names like that. But, for third party applications you need to specify the fully qualified names.

Path: Path indicates the path to the data you want from the provider. It can be a table name or something else. You are free to name it whatever you like for your application. But for most of the cases it is sensible to name it according to table names if your are storing data in databases. If you are contacting a third party application then look at their documentation to see what name they have provided.

ID: To uniquely identify a single piece of data inside an application we use this part of the URI. You can think it like the primary key of a row of data in a relational database. This part is optional.

Content Provider And Its Methods

To use content providers you need to create a class that extends the ContentProvider class, and override the following methods and provide your own implementation.

onCreate(): This method is called when the provider is created and ready to work.

query(): When a client application requests some data, this method is called and a Cursor object is returned to the client. This methods is an interface to the read operation with the help of Content Provider. This is the R of CRUD.

insert(): This method is used to add data to the applications. It is the C of CRUD.

delete(): This method is used to delete an existing record from the data source of the Content Provider hosting application. This is used for the D from CRUD.

update(): The update() method is used to update an existing record from the data source of the content provider hosting application. It is obviously the U from the CRUD.

getType(): This method is used for getting the MIME type of the data at the given URI.

Content Resolver And Its Methods

Content providers act when they are asked to. But another application cannot make it work by trying to call the class in some silly way. It is not possible to use a class and instantiate that from another external application. The client application needs to use Content Resolver.

Content Resolver is the single instance in your application that provides access to the Content Providers. Usually, to get the content resolver you need to call the method getContentResolver() on a context or from inside of a context. You need to use the content resolver to call various methods on it that do the CRUD operations on the desired content provider.

Conclusions

SQLite being the default database system of Android, there is no client-server model of data communication between applications. And, for the same reason, along with data security, two different applications cannot use the same SQLite database. This is a hindrance for building cooperative applications. The disadvantage created by the serverless model of SQLite is removed by Content Providers and they help exchange data between applications in a controlled manner. But this is not the end of the story and it is not only limited to SQLite. You can use any data source you want to use as the backend of the Content Provider. Even you can fetch data from the internet and share it with other applications with Content Provider in real time.

This article was mainly a theoretical discussion for a better understanding Content Providers. Check back for a future article that will show you how to work with Content Providers with a real application as the example.

By Md. Sabuj Sarker | 10/30/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now