Android中的Intent vs Content Provider

问题描述:

我是android应用程序开发的新手,在研究基本的android组件时,我在意图和内容提供者之间感到困惑,因为两者都用于将数据从一个应用程序/组件发送到另一应用程序/组件.在有意向的情况下,我们可以使用捆绑包或附加内容发送数据,因此为什么要使用内容提供者.有人可以举例说明一下.

我们也可以仅使用内容提供程序访问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 ?

两者都用于将数据从一个应用程序/组件发送到另一应用程序/组件

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

不是真的.

Android中有四个组件:

There are four components in Android:

  • 活动
  • 服务
  • BroadcastReceiver
  • ContentProvider

Intent 都不是.当我们启动活动,启动或绑定到服务或发送广播时,会涉及到 Intent .但是,将 Intent 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.

四个组件中的每个都有不同的作用,尤其是在与进程间通信(IPC)的关系上:

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

  • Activity 驱动了我们大部分的用户界面,包括从其他应用程序启动活动(或让我们的活动之一由其他应用程序启动)

  • 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)

Service 用于长时间运行的操作,这些操作在逻辑上与用户界面分离,包括使用由其他应用程序实现的服务(或使其他应用程序与您所使用的服务一起使用)发布)

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)

BroadcastReceiver 是一种发布/订阅消息传递系统,允许您跨进程边界将消息发送给任意订阅者,或订阅来自任意发件人的消息

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

ContentProvider 用于批量数据传输,无论是以数据库样式结构(行和列)的形式还是以流的形式,尤其是与其他应用一起使用时

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

我们也只能使用内容提供商访问android中的数据库

Also can we access database in android only using content provider

不.毕竟,如果这是真的,那么将不可能访问数据库. ContentProvider 不会神奇地出现.它必须由程序员编写.如果 ContentProvider 只能通过 ContentProvider 来访问数据库,那么我们将有问题.

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 ?

不.除了提供数据库样式的API外, ContentProvider 还可以发布流.这对于在应用程序之间获取任意数据非常重要,例如电子邮件客户端,使PDF附件可供PDF查看器使用.

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.