Nega developer
Nega developer

Reputation: 269

OnResquestPermissionResult from Adapter

I Have a BaseAdapter with a List of all requires permissions for use correctly the app.

I need to, after onClick in one of them, request the permission and check if is granted or not.

For request, I am using

ActivityCompat.requestPermissions

but I do not know how to get the results.

Is it possible to call O

nRequestpermissionsResultCallback

to know the answer of the user?

How can I do it?

Upvotes: 4

Views: 8305

Answers (5)

Moeenuddin178
Moeenuddin178

Reputation: 71

so first i implement in the Adapter implements ActivityCompat.OnRequestPermissionsResultCallback then it require one Overide method

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        //  it is not work here  onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == storagePermission) {

            // getPosts(false);

            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(context.getApplicationContext(), "Please allow permission to Continue", Toast.LENGTH_SHORT).show();



            }

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              Toast.makeText(context.getApplicationContext(), " Permissions Granted Succecfully", Toast.LENGTH_SHORT).show();

        //here i load my method  that i want to run 
            }

        }
    }

now problem is this onRequestPermissionsResult is not call from adapter instead adapter triger onRequestPermissionsResult of the mainActivity Attach with the Adapter

so in main Activity i create this and run onRequestPermissionsResult of adapter by passing adapterPosts.onRequestPermissionsResult(requestCode,permissions,grantResults);

  @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == storagePermission) {

            // getPosts(false);

            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
           //permission not granted
            }

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               //permission granted

                adapterPosts.onRequestPermissionsResult(requestCode,permissions,grantResults);
                //loadImageIntent();
            }

        }
    }

Upvotes: 2

S. Koshelnyk
S. Koshelnyk

Reputation: 506

I solved this from custom class. My custom class is derived from AppCompatActivity. Also you need to pass Activity context to your custom class and do whatever you need with it.

public bool AreStorageAndCamPermissionsGranted(Activity context)
        {
            if (Build.VERSION.SdkInt >= Build.VERSION_CODES.M)
                if (
                            context.CheckSelfPermission(Manifest.Permission.Camera)!= Android.Content.PM.Permission.Granted
                          || context.CheckSelfPermission(Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted
                          || context.CheckSelfPermission(Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(context, new String[]
                   {
                                Manifest.Permission.Camera,
                                Manifest.Permission.ReadExternalStorage,
                                Manifest.Permission.WriteExternalStorage,
                   }, REQUEST_PERMISSION_CODE);
                }
            return true;
        }

`

Upvotes: 0

Meisam Salami
Meisam Salami

Reputation: 68

I have a same problem in RecylclerView.Adapter and I use this section in permission request method

private void requestCallPhonePerm() {
    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {
        onItemCallListener.itemCallPhone(itemPhones);
        ActivityCompat.requestPermissions((Activity)context,
                new String[]{Manifest.permission.CALL_PHONE},
                MY_PERMISSIONS_REQUEST_CALL_PHONE);
    } else {
        ((MainActivity)context).callPhone(itemPhones);
    }
}

It's important to use (Activity)context in "requestPermissions" method to be invoked callback in container activity of your adapter

And in my MainActivity that contain recycler I use overrided request permission result normally.

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch(requestCode){
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
                if(grantResults.length>0
                        &&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    new LoadContacts().execute();
                }
                break;
            case MY_PERMISSIONS_REQUEST_CALL_PHONE:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    callPhone(phones);
                }else{

                }
        }
    }

Upvotes: 0

Lovish
Lovish

Reputation: 11

In your adapter class

class Adapter extends RecyclerView.Adapter<Your adapter.MyViewHolder> implements ActivityCompat.OnRequestPermissionsResultCallback{`//enter code here`}

It will override the onRequestPermissionsResult

Upvotes: 0

Archlin
Archlin

Reputation: 11

To check the permission (in this example it's a permission to use the camera):

   int permissionCheck = ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA);
   if (permissionCheck == PackageManager.PERMISSION_DENIED) {
            // permission is not granted yet, let's ask for it
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    MyActivity.cameraRequestCode);
   } else {
            // permission is already granted
   }

If ActivityCompat.requestPermissions is run, then a dialogue appears for the user, after which you can catch the result by overriding the Activity's onRequestPermissionsResult like this:

@Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case MyActivity.cameraRequestCode: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   // action when permission granted
                } else {
                    // action when permission denied
                }
                return;
            }
        }
    } 

MyActivity.cameraRequestCode is a static int, can be any unique int value.

Upvotes: 0

Related Questions