Not able to grant my custom content provider permission in android?
Asked Answered
G

1

2


I was developing a content provider app. In the manifest of that app I was placed a provider element In the application tag. The following are the code

<provider
            android:name=".PlatesContentProvider"
            android:authorities="com.tag.custom_contentproviderdemo.Plates"
            android:enabled="true"
            android:readPermission="PlatesContentProvider._READ_PERMISSION"
            android:writePermission="PlatesContentProvider._WRITE_PERMISSION"
            android:exported="true" >



I also developed another app namely CPClient. It will read/write data to the content provider mentioned above. I was declared element in its manifest file.

The following are the code

<uses-permission android:name="PlatesContentProvider._READ_PERMISSION"/>
    <uses-permission android:name="PlatesContentProvider._WRITE_PERMISSION"/>


In the launcher activity of the CPClient I was checked for this permission .If it is not granted means I will request for permission. But in requesting permission it does not shown request permission dialog and always permission is not granted.

The following are the code

private final String permissionsRequired="PlatesContentProvider._READ_PERMISSION";
    private final String permissionsRequired2= "PlatesContentProvider._WRITE_PERMISSION";
    private String[] permissions=new String[]{permissionsRequired,permissionsRequired2};    
public void process()
        {
            checkPermission();

            if(!permission_granted)
            {
                Toast.makeText(this,"Permission not granted.Requesing permission....",Toast.LENGTH_SHORT).show();
                requestPermission1();return;
            }
            else
            {
                bindWidgetEvents();  //After permission granted I will perform some process
                Log.d(TAG,"Permission granted");return;
            }
        }

        public void checkPermission()
        {
            int permit=this.checkSelfPermission(permissionsRequired);

            if(permit== PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "permission granted");
                permission_granted=true;return;
            }
            else
                Log.d(TAG,"permission not granted");
            permission_granted=false;
        }

        public void requestPermission1()
        {
            //boolean show=this.shouldShowRequestPermissionRationale(permissionsRequired);
            //Log.d(TAG,"need to show = "+show);


            Thread thread=new Thread()
            {
                public void run()
                {
                    Log.d(TAG,"B4 call request permission");
                    requestPermissions(permissions,CONTENT_PROVIDER_PERMISSION_REQUEST_CODE);

                    Log.d(TAG,"after call request permission");
                }
            };

            thread.start();
        }
    public void onRequestPermissionsResult(int requestCode,String permissions1[], int[] grantResults)
        {
            Log.d(TAG,"onRequestPermissionsResult. req code="+requestCode);
            if(requestCode==CONTENT_PROVIDER_PERMISSION_REQUEST_CODE)
            {
                Log.d(TAG,"Content provider permsion request code");
                if(grantResults==null)
                {
                    Log.d(TAG,"grant results is null");
                    Toast.makeText(this,"UnKnown error happened",Toast.LENGTH_LONG).show();
                    return;
                }

                int grant_results_size=grantResults.length;
                Log.d(TAG,"Grant result size="+grant_results_size);
                if(grant_results_size<1)
                {
                    Log.d(TAG,"grant results size is <1");
                    Toast.makeText(this,"UnKnown error happened",Toast.LENGTH_LONG).show();
                    return;
                }

                if(grantResults[0]!=PackageManager.PERMISSION_GRANTED)
                {
                    Log.d(TAG,"Permission not granted");
                    Toast.makeText(this,"Read Permission not granted",Toast.LENGTH_LONG).show();
                }

                if(grantResults[1]!=PackageManager.PERMISSION_GRANTED)
                {
                    Log.d(TAG,"Permission not granted");
                    Toast.makeText(this,"Write Permission not granted",Toast.LENGTH_LONG).show();return;
                }

                Log.d(TAG,"Permission granted");

                Toast.makeText(this,"Permission granted",Toast.LENGTH_SHORT).show();
                bindWidgetEvents();

                return;
            }
            else
            {
                Log.d(TAG,"not a Content provider permsion request code");
            }
        }


The following are the android sdk details my target and min sdk version is 23 in the both content provider app and my CPClient.
I don't know where I will go wrong?
All are welcome to give their ideas.

Gesticulate answered 3/2, 2017 at 10:30 Comment(5)
Have you declared <permission> elements for those custom permissions in the manifest for the app with the Provider? https://mcmap.net/q/663815/-how-does-content-provider-39-s-application-specify-permissions-that-client-apps-need-in-order-to-access-the-provider-39-s-dataDissolute
im not added.plz update the sample code for thatGesticulate
I'm sorry, I don't understand your comment.Dissolute
im not added permission element .send me details where i need to add in the manifest.its very usefull for meGesticulate
The answer below shows where to put them. They go outside of the <application> tags in the manifest for the app with the <provider>.Dissolute
W
5

You should declare your permissions in 'manifest' block as well as in provider.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package">

    <permission
        android:name="PlatesContentProvider._READ_PERMISSION"
        android:protectionLevel="normal" />
    <permission
        android:name="PlatesContentProvider._WRITE_PERMISSION"
        android:protectionLevel="normal" />
    <application>
        <provider
            android:name=".PlatesContentProvider"
            android:authorities="com.tag.custom_contentproviderdemo.Plates"
            android:enabled="true"
            android:readPermission="PlatesContentProvider._READ_PERMISSION"
            android:writePermission="PlatesContentProvider._WRITE_PERMISSION"
            android:exported="true" >
    </application>
</manifest>
Wavy answered 3/2, 2017 at 10:38 Comment(2)
Its not working for .In the client app when I call requestPermissions it does not show any dialog and always return permission is not granted.Gesticulate
If you declare this permissions as 'normal' level protection, they are automatically granted. You can also use 'signature' or 'signatureOrSystem' protection levels. Please refer to developer.android.com/guide/topics/manifest/….Deepfreeze

© 2022 - 2024 — McMap. All rights reserved.