1

I want to prompt the user for camera permissions before it changes activities. If the user allows then it will go to the QR code scanner and if the user denied it will return to the same screen.

Here is what I have at the moment

 class ButtonGoToScannerClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                ActivityCompat.requestPermissions(BaseMapActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        1);
                Intent intent = new Intent(BaseMapActivity.this, BarCodeReaderActivity.class);
                startActivityForResult(intent, ITEM_LOCATION_REQUEST);
            }
        }
    }
Phantômaxx
  • 37,352
  • 21
  • 80
  • 110

1 Answers1

1

Here is what you can do, first request for the permission

 ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.CAMERA},
                1);

and now check if the permission is allowed or not

@Override
public void onRequestPermissionsResult(int requestCode,
                                   String permissions[], int[] grantResults) {
switch (requestCode) {
    case 1: {

      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent intent = new Intent(BaseMapActivity.this, BarCodeReaderActivity.class);
            startActivityForResult(intent, ITEM_LOCATION_REQUEST);
            // 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.
            Toast.makeText(MainActivity.this, "Permission denied for camera", Toast.LENGTH_SHORT).show();
        }
        return;
    }

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

Hope this helps

Abdul Kawee
  • 2,666
  • 1
  • 13
  • 26