Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent vs Content Provider in android

Tags:

android

I am new in android app development and while studying about the basic android components I got confused between intents and content provider as both are being used to send data from one application/component to another application/component . In case of intents we can send data using bundle or extras so why do we use content providers. Can someone please explain me this with a example .

Also can we access database in android only using content provider and is this the sole reason why we use content providers ?

like image 589
Shivam Aggarwal Avatar asked Aug 08 '15 19:08

Shivam Aggarwal


People also ask

Can content provider start from Intent?

The Intent object is a common mechanism for starting new activity and transfering data from one activity to another. However, you cannot start a ContentProvider using an Intent.

What is Intent provider in Android?

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc.

Why do we use content provider in Android?

Content providers can help an application manage access to data stored by itself, stored by other apps, and provide a way to share data with other apps. They encapsulate the data, and provide mechanisms for defining data security.


2 Answers

both are being used to send data from one application/component to another application/component

Not really.

There are four components in Android:

  • Activity
  • Service
  • BroadcastReceiver
  • ContentProvider

An Intent is none of those. An Intent is involved when we start an activity, start or bind to a service, or send a broadcast. However, comparing an Intent to a ContentProvider is akin to comparing a shovel with a boot, arguing that both can be used to carry dirt. While true, usually a boot is involved in carrying dirt, but the actual means of carrying dirt is handled by something else, such as a wheelbarrow.

In case of intents we can send data using bundle or extras so why do we use content providers.

We often use different tools for different circumstances. For example, you will find it rather difficult to carry water in a fishing net.

Each of the four components has a different role, particularly in relationship to inter-process communication (IPC):

  • An Activity drives the bulk of our user interface, including starting up activities from other apps (or having one of our activities be started by other apps)

  • A Service exists for longer-running operations that are logically decoupled from the user interface, including working with services that are implemented by other apps (or having other apps work with services that you publish)

  • A BroadcastReceiver is a publish/subscribe messaging system, to allow you to send messages to arbitrary subscribers, or to subscribe to messages from arbitrary senders, across process boundaries

  • A ContentProvider is for bulk data transfer, whether in the form of a database-style structure (rows and columns) or in the form of a stream, particularly for working with other apps

Also can we access database in android only using content provider

No. After all, if that were true, it would be impossible to access a database. A ContentProvider does not appear by magic. It has to be written by a programmer. If a ContentProvider could only access a database by means of a ContentProvider, we would have a problem.

is this the sole reason why we use content providers ?

No. In addition to offering a database-style API, a ContentProvider can also publish a stream. This is important for getting arbitrary data between apps, such as an email client making a PDF attachment available to a PDF viewer.

like image 20
CommonsWare Avatar answered Oct 08 '22 16:10

CommonsWare


Intents are a messaging architecture for sending /receiving transactional commands and data. Content providers are an abstract interface to stored data for create,update, delete and sync operations.

like image 94
John Fontaine Avatar answered Oct 08 '22 15:10

John Fontaine