如何实现只读ContentProvider?

发布于 2025-01-01 16:30:02 字数 295 浏览 1 评论 0原文

我想知道如何最好地实现只读 ContentProvider。 我希望仅通过 ContentProvider 的其他特殊方法(当然无法通过 ContentResolver 访问)从我自己的应用程序内修改我的数据源。 换句话说,其他应用程序应该只能使用我的ContentProvider的查询方法,而不能插入、删除或更新。

显而易见的解决方案似乎是只返回 null/0/0 并且在插入/删除/更新中不执行任何其他操作。是否总是在这些方法中抛出异常以便清楚地传达这些操作是不允许的?或者是否有可能仅通过权限将对 ContentProvider 的访问限制为查询方法?

I am wondering how to best implement a read-only ContentProvider.
I want my data source to be modified only from within my own application through additional special methods of my ContentProvider (which of course are not accessible through a ContentResolver).
In other words, other applications should only be able to use my ContentProvider's query method but not insert, delete, or update.

The obvious solution seems to be to just return null/0/0 and do nothing else in insert/delete/update. Would it be better to always throw an Exception in these methods instead so as to communicate clearly that these operations are not allowed? Or is there even a possibility of restricting the access to the ContentProvider to the query method only via permissions?

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

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

发布评论

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

评论(2

初见你 2025-01-08 16:30:02

实现此目的的一种方法是通过安全权限,您可以通过 此链接< /a> 在 ContentProvider 段落中。具体来说,您可以在 AndroidManifest 中的提供程序上设置 writePermission xml 文件。

但是,如果您不想使用安全权限,则可以使用第二段中提到的方法。我建议抛出异常,以便清楚地表明无法访问那些特定的插入/更新/删除功能。

One method to accomplish this is via security permissions which you can access at this link in the ContentProvider paragraph. Specifically, you would set a writePermission on your provider in your AndroidManifest xml file.

If you do not wish to use security permissions, however, you can use the approaches mentioned in your second paragraph. I would suggest throwing exceptions so that it is clear that those particular insert/update/delete features can't be accessed.

梦在深巷 2025-01-08 16:30:02

两年后,我问自己同样的问题。
我知道权限就是答案。

尽管如此,您必须在“插入/删除/更新”方法中编写一些内容(希望不会被调用)。

我同意使用异常,因为它不应该被调用,如果被调用,则必须警告您。

此处发现一行内容如下:

虽然您必须实现这些方法,但您的代码不必
除了返回预期的数据类型之外,执行任何操作。例如,您可以
想要阻止其他应用程序将数据插入到某些应用程序中
表。为此,您可以忽略对 insert() 的调用并返回 0。

也就是说,最好的方法就是返回 null/0/0。我要用这个方法

Two years later, I'm asking myself the very same question.
I understand that permissions are the answer.

Nevertheless, you have to write something inside the "insert/delete/update" methods (that hopefully will not be callable).

I would agree on using an exception because, as it is not supposed to be call, you must be warned if it is.

But a line found here says:

Although you must implement these methods, your code does not have to
do anything except return the expected data type. For example, you may
want to prevent other applications from inserting data into some
tables. To do this, you can ignore the call to insert() and return 0.

That would say the good way to do is just to return null/0/0. I'm gonna use this way.

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