如何getContentResolver()的工作?工作、getContentResolver

2023-09-04 10:08:44 作者:孤鸿

我已经学到了一些课程有关从互联网内容提供商。这个例子说明如何定义和使用ContentProvider的。

I had learned some course about Content Provider from Internet. The example shows how to define and use a ContentProvider.

不过,我有一个迷惑的是关于使用 getContentResolver()这种方法命名,这是什么方法回报?

But, I had a confuse that is about using this method named getContentResolver() ,What is this method return?

我的ContentProvider不instanced.and的code只写 getContentProvider()查询()

My ContentProvider is not instanced.and the code just write that getContentProvider().query().

我不明白它是如何工作的。

I don't understand how does it work.

推荐答案

它返回的内容解析器。

It returns Content Resolver.

什么是内容解析器?

的内容解析器是应用程序的单一的全球性实例,提供访问您的(和其他应用程序)的内容提供商。内容解析器的行为完全因为它的名字所暗示的:它接受来自客户端的请求,并指导他们的内容供应商,具有鲜明的权威解析这些请求。要做到这一点,内容解析器存储的映射,从当局内容提供商。这种设计是非常重要的,因为它允许访问其它应用程序的内容提供者的一个简单而安全的手段。

The Content Resolver is the single, global instance in your application that provides access to your (and other applications') content providers. The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers. This design is important, as it allows a simple and secure means of accessing other applications' Content Providers.

该内容解析器包括CRUD(创建,读取,更新,删除)对应的抽象方法(插入,删除,查询,更新)中的内容提供商类的方法。内容解析器不知道内容提供商将其与交互(也不需要知道)的实施;每个方法传递一个URI,指定内容提供商进行交互。

The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, delete, query, update) in the Content Provider class. The Content Resolver does not know the implementation of the Content Providers it is interacting with (nor does it need to know); each method is passed an URI that specifies the Content Provider to interact with.

什么是内容提供商?

而内容解析器提供从应用程序的内容提供者的抽象,内容提供商提供了从底层数据源的抽象(即 SQLite数据库)。它们提供了用来定义数据的安全性(通过执行读/写权限IE),并提供了一​​个过程,code在另一个进程中运行数据连接的标准接口。

Whereas the Content Resolver provides an abstraction from the application's Content Providers, Content Providers provides an abstraction from the underlying data source (i.e. a SQLite database). They provide mechanisms for defining data security (i.e. by enforcing read/write permissions) and offer a standard interface that connects data in one process with code running in another process.

内容提供商提供发布和使用数据提供一个接口,基于一个简单的URI中使用的内容模型://模式。它们使您能够通过提取底层数据源从基础数据层中分离出应用层,使应用程序的数据源无关。

Content Providers provide an interface for publishing and consuming data, based around a simple URI addressing model using the content:// schema. They enable you to decouple your application layers from the underlying data layers, making your application data-source agnostic by abstracting the underlying data source.

来源 - androiddesignpatterns