How to implement read-only ContentProvider?

I am wondering what is the best way to implement a read-only ContentProvider. I want my data source to be modified only from my own application using the special special methods of my ContentProvider (which, of course, are not available through ContentResolver). In other words, other applications should use my ContentProvider method, but not insert, delete, or update.

The obvious solution is to simply return null / 0/0 and do nothing in insert / delete / update. Would it be better to always exclude an exception in these methods in order to clearly communicate that these operations are not allowed? Or is it possible to restrict access to the ContentProvider request method only through permissions?

+6
source share
2 answers

One way to achieve this is through security permissions, which can be accessed in this link in the ContentProvider paragraph. In particular, you must install writePermission on your provider in your AndroidManifest XML file.

If you do not want to use security permissions, you can use the approaches mentioned in your second paragraph. I would suggest eliminating exceptions so that it is clear that these specific insert / update / delete functions cannot be accessed.

+3
source

Two years later, I ask myself the same question. I understand that permissions are the answer.

However, you need to write something inside the "insert / delete / update" methods (which I hope will not be called).

I would agree to use an exception, because since it should not be a challenge, you should be warned if there is one.

But a line based here : says

Although you must implement these methods, your code should not do anything other than return the expected data type. For example, you might want other applications not to insert data into some tables. To do this, you can ignore the insert () call and return 0.

This suggests that a good way is to simply return null / 0/0. I will use this way.

I am not sure whether it is worth wasting time on such a secondary question.

+3
source

Source: https://habr.com/ru/post/907347/


All Articles