How to get Camera permission is enable in app permissions in android app?

13,124

Request the permissions you need

If your app doesn't already have the permission it needs, the app must call one of the requestPermissions() methods to request the appropriate permissions. Your app passes the permissions it wants, and also an integer request code that you specify to identify this permission request. This method functions asynchronously: it returns right away, and after the user responds to the dialog box, the system calls the app's callback method with the results, passing the same request code that the app passed to requestPermissions().*

int MY_PERMISSIONS_REQUEST_Camera=101;
// Here, thisActivity is the current activity
if (ContextCompat.CheckSelfPermission(thisActivity,
                Manifest.Permission.Camera)
        != Permission.Granted) {

    // Should we show an explanation?
    if (ActivityCompat.ShouldShowRequestPermissionRationale(thisActivity,
            Manifest.Permission.Camera)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.RequestPermissions(thisActivity,
                new String[]{Manifest.Permission.Camera},
                MY_PERMISSIONS_REQUEST_Camera);

        // MY_PERMISSIONS_REQUEST_Camera is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Handle the permissions request response

When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's OnRequestPermissionsResult() method, passing it the user response. Your app has to override that method to find out whether the permission was granted. The callback is passed the same request code you passed to requestPermissions(). For example, if an app requests Camera access it might have the following callback method

public override void OnRequestPermissionsResult(int requestCode, 
          string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_Camera: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.Length > 0 && grantResults[0] == Permission.Granted) {

                // permission was granted, yay! Do the
                // camera-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

above example based on google original permission documentions

Share:
13,124
anand
Author by

anand

Updated on June 04, 2022

Comments

  • anand
    anand almost 2 years

    I am working on android app in xamarin plateform. I have enabled the camera functionality for app from app manifest. After running the app user disable the camera from app permissions screen. So how I can get that user has disable this functionality from app permission?

    I am trying following code to get it but every time I am getting only "Granted" in result. If user disable the permission then I should get "Denied" in result.

     var val = PackageManager.CheckPermission (Android.Manifest.Permission.Camera, PackageName);
    

    enter image description here

  • anand
    anand about 8 years
    Thanks for reply. I have installed it. Do you have any idea what code I should use to get the permission results?
  • anand
    anand about 8 years
    Thanks for reply Giorgi. But Permission.Camera is not available. Under Permission only two values Granted and Denied is available. I tried with Manifest.Permission.Camera but its throwing error that it is not correct value that should paas in this method.
  • anand
    anand about 8 years
    I have done the coding part but still I am getting "Granted" value in every case. If I disable the Camera permission from App permission screen then still I am getting Granted in result. Do you have any idea what may I am doing wrong?