How does Content Provider's application specify permissions that client apps need in order to access the provider's data?
C

2

14

BACKGROUND

I am reading this tutorial on Android Content Providers. I understand from this tutorial that,

In order for other applications to access a Content Provider's data, the provider application must specify the permissions which the client applications need to have to access the its provider's data.

Client applications specify the permissions they require in their manifest file using the <uses-permission> element, e.g.

<uses-permission android:name="android.permission.READ_USER_DICTIONARY" > <!-- In the client app's manifest -->

Then the APK manager asks the user's consent on these permissions (in the client app) when the user is installing the client application.

QUESTION

My question is that how does the provider (app) specify the permissions that other client apps must be granted in order for them to access the provider's data?

From the developer guide,

To find the exact name of the read access permission for the provider you're using, as well as the names for other access permissions used by the provider, look in the provider's documentation.

So is that the way to specify those permissions in the provider app - in the provider's documentation? If so, where is that documentation found? Where can I find that documentation for the SearchableDictionary provider (used as an example in the tutorial), and if I write a Content Provider in my app, where shall I provide that documentation?

Codpiece answered 7/5, 2015 at 9:43 Comment(0)
E
33

Define Permission in provider app's AndroidManifest.xml

<permission
    android:name="com.myapp.PERMISSION"/>

Define Provider in provider app's AndroidManifest.xml

<provider
        android:name=".MyProvider"
        android:authorities="com.myapp.MyProvider.AUTHORITY"
        android:enabled="true"
        android:exported="true"
        android:multiprocess="true"
        android:readPermission="com.myapp.PERMISSION" />

Client's AndroidManifest.xml should have uses-permission tag

<uses-permission android:name="com.myapp.PERMISSION"/>

Then client can access the provider

Cursor cursor = getContentResolver().query(
Uri.parse("content://com.myapp.MyProvider.AUTHORITY/xxx" ),null, null, null, null);
Etude answered 7/5, 2015 at 11:33 Comment(5)
Thank you so very much for a very clear answer. But I am still slightly confused about the quote I quoted from the developer guide about provider's documentation: what documentation are they talking about? Do you have any idea?Codpiece
But I am clear now that I can look into a provider app's manifest to know the name of the permission. So thank you.Codpiece
@Codpiece i had exactly your problem, can u pls tell me where had you find in this example UserDictionary's manifest file???Quaternion
Like this same I was followed.I was created one app for content provider (act as a server) and other app for accessing data (act as a client).I was also created permissions and used in manifest also.but in the client app the permissions is always not granted. Go to the link and see the details https://mcmap.net/q/671270/-not-able-to-grant-my-custom-content-provider-permission-in-android/385138Yount
This is exactly the solution I was looking for. Thanks man! :DParlous
C
4

And don't forget in the client (B app) to include this in AndroidManifest.xml , outside .... This works for me

<queries>
        <provider android:authorities="com.myapp.MyProvider.AUTHORITY" />
    </queries>
Creepie answered 23/11, 2021 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.