ContentResolver如何找到对应的ContentProvider?

发布于 2024-10-10 20:16:15 字数 286 浏览 5 评论 0原文

这是一个深奥的魔法问题。 我知道对 ContentResolver 方法的调用需要特定于 ContentProvider 的 URI,但是 android 实际上如何进行关联?

我猜测涉及与 AndroidManifest.xml 中 ContentProvider 提供的权限匹配的任何 URI。 请求是否发送给每个包含该权限的提供商? 如果我尝试创建其权限以另一个权限为前缀的提供者,这会是一个问题吗?

有没有办法查看ContentProvider是否正在运行?我想也许 getType() 方法上的虚拟响应会表明活跃度。

This is a deep magic question.
I understand that a call to a ContentResolver method takes a URI specific to the ContentProvider, but how does android actually make the association?

I am guessing that any URI matching the authority provided with the ContentProvider in the AndroidManifest.xml is involved.
Is the request sent to every provider containing that authority?
If I try to create providers whose authority prefixes another authority will that be a problem?

Is there a way to see if the ContentProvider is running? I'm thinking maybe a dummy response on the getType() method would indicate liveness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2024-10-17 20:16:15

ContentResolver 类维护从内容权限到 ContentProvider 类的映射。该映射的数据来自各种已安装应用程序的 AndroidManifest.xml 文件的 元素。 ContentResolver 使用此映射来识别哪个 Provider 类适合用于传入的给定 URI。将 ContentResolver 视为有点像 DNS。它会找出哪个服务器(提供商)最适合回答您的查询。

只有一个 ContentProvider 会匹配,因为 contentAuthorities(内容的“域名”部分:类型 uri)要求是唯一的。他们没有等级之分。将它们视为必须完全匹配的唯一字符串。它们看起来具有层次结构的原因是为了提供一种保证唯一性的简单方法,类似于确保 Java 包名称唯一的方式。

根据标签文档的“描述:”部分:

Android系统识别内容
提供者由当局的一部分
内容:URI。例如,假设
以下 URI 被传递到
ContentResolver.query():

content://com.example.project.healthcareprovider/nurses/rn

内容:方案标识
数据属于内容
提供者和当局
(com.example.project.healthcareprovider)
识别特定的提供者。
因此,当局必须
独特的。通常,如本例所示,
这是一个完全限定的名称
ContentProvider 子类。路径
URI 的一部分可以被内容使用
提供者识别特定数据
子集,但这些路径不是
在清单中声明

至于当您创建一个具有与另一个相同的 contentAuthority 的提供程序时会发生什么......好吧,东西坏了。具体来说,它将拒绝安装第二个软件包,并表示:

警告/PackageManager:无法安装,因为提供程序名称为 com.xxx.Provider
(在 com.xxx 包中)已被 com.zzz 使用

所以......不要这样做。

无法查看 ContentProvider 是否正在运行。它由 ContentResolver 根据需要自动启动和停止。当您开始向特定 contentAuthority 发出请求时,关联的提供程序(如果尚未运行)将会启动。一旦它闲置一段时间后,它会被 ContentResolver 自动停止,并且看起来可能暂时不需要它。

Class ContentResolver maintains a mapping from Content Authorities to ContentProvider classes. The data for that mapping comes from the <provider> elements of the various installed applications' AndroidManifest.xml files. ContentResolver uses this mapping to identify which Provider class is the right one to use for a given URI that comes in. Think of ContentResolver as being sort of like DNS. It figures out which server (provider) is the right one to answer your query.

Only one ContentProvider will match, because contentAuthorities (the "domain name" part of the content: type uri) are required to be unique. They are not hierarchical. Treat them as a unique string which must exactly match. The reason they look hierarchical is to allow an easy way of guaranteeing uniqueness, akin to the way Java package names are ensured to be unique.

Per the 'Description:" section for the tag documentation:

The Android system identifies content
providers by the authority part of a
content: URI. For example, suppose
that the following URI is passed to
ContentResolver.query():

content://com.example.project.healthcareprovider/nurses/rn

The content: scheme identifies the
data as belonging to a content
provider and the authority
(com.example.project.healthcareprovider)
identifies the particular provider.
The authority therefore must be
unique. Typically, as in this example,
it's the fully qualified name of a
ContentProvider subclass. The path
part of a URI may be used by a content
provider to identify particular data
subsets, but those paths are not
declared in the manifest

As for what happens when you make a provider with a contentAuthority that's identical to another one... Well, stuff breaks. Specifically, it will refuse to install whichever package goes on second, saying:

WARN/PackageManager: Can't install because provider name com.xxx.Provider
(in package com.xxx) is already used by com.zzz

So.... Don't do that.

There is no way to see if the ContentProvider is running. It is started and stopped automatically by ContentResolver as needed. When you start making requests for a specific contentAuthority, the associated provider will be started if it isn't already running. It will be stopped automatically by ContentResolver, some time later once it has sat idle and it looks like it might not be needed for a while.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文