如何实现只读ContentProvider? [英] How to implement read-only ContentProvider?

查看:144
本文介绍了如何实现只读ContentProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何最好地实现只读ContentProvider. 我希望仅通过我的ContentProvider的其他特殊方法(当然不能通过ContentResolver对其进行访问)从自己的应用程序中修改数据源. 换句话说,其他应用程序只能使用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.

显而易见的解决方案似乎只是返回null/0/0,而在insert/delete/update中什么也不做.总是在这些方法中抛出Exception更好,以便清楚地表明不允许执行这些操作,这会更好吗?还是有可能仅通过权限将对ContentProvider的访问限制为查询方法?

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?

推荐答案

一种实现此目的的方法是通过安全权限,您可以在 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.

这篇关于如何实现只读ContentProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆