如何实现只读ContentProvider?
我想知道如何最好地实现只读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现此目的的一种方法是通过安全权限,您可以通过 此链接< /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.
两年后,我问自己同样的问题。
我知道权限就是答案。
尽管如此,您必须在“插入/删除/更新”方法中编写一些内容(希望不会被调用)。
我同意使用异常,因为它不应该被调用,如果被调用,则必须警告您。
但此处发现一行内容如下:
也就是说,最好的方法就是返回 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:
That would say the good way to do is just to return null/0/0. I'm gonna use this way.