ShadowsEdge19
Newbie
Hi,
I have a simple app for my android 7.0 test device (LG G6) that presses a button called Scan that activates the ZXing Barcode scanner implemented in my app build.gradle:
But before that I needed to get permission from the user to activate the camera for the app, so I have the Manifest.xml listing the permission required:
Next I have the MainActivity activity that extends AppCompartActivity implements OnClickListener for the scan button click event:
I have a permission request code in my class declarations as well as a global boolean to determine if I have permission.
Then in my onClick() I check for the scan button click, then the permission check
I then have a onRequestPermissionsResult() callback which should be called when I use ActivityCompat.requestPermission() and set cameraPermissionGranted, but it is ignored completely.
I am not using Fragments so I shouldn't need to reference requestPermissions() without ActivityCompat, nor can I as I get an error when I take it off.
So why am I not able to override the onRequestPermissionResult() method?
I have a simple app for my android 7.0 test device (LG G6) that presses a button called Scan that activates the ZXing Barcode scanner implemented in my app build.gradle:
Code:
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
implementation 'com.google.zxing:core:3.2.0'
But before that I needed to get permission from the user to activate the camera for the app, so I have the Manifest.xml listing the permission required:
Code:
<uses-permission android:name="android.permission.CAMERA" />,
Next I have the MainActivity activity that extends AppCompartActivity implements OnClickListener for the scan button click event:
Code:
public class MainActivity extends AppCompatActivity implements OnClickListener {
..
}
I have a permission request code in my class declarations as well as a global boolean to determine if I have permission.
Code:
private static final int MY_PERMISSIONS_REQUEST_CAMERA = 100;
private boolean cameraPermissionGranted = false;
Then in my onClick() I check for the scan button click, then the permission check
Code:
@override
public void onClick(View v) {
//scan
if(v.getId()==R.id.scan_button){
boolean bHasPermission = false;
// Here, this is the current activity
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA))
{
if (TextUtils.isEmpty("")) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA );
}
else{
// Show rationale message
}
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA );
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else{
cameraPermissionGranted = true;
}
if (cameraPermissionGranted)
{
// Already have permission
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
// Results of the scan are then retrieved via the onActivityResult callback method below
}
}
}
I then have a onRequestPermissionsResult() callback which should be called when I use ActivityCompat.requestPermission() and set cameraPermissionGranted, but it is ignored completely.
Code:
@override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cameraPermissionGranted = true;
} else {
cameraPermissionGranted =false;
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
I am not using Fragments so I shouldn't need to reference requestPermissions() without ActivityCompat, nor can I as I get an error when I take it off.
So why am I not able to override the onRequestPermissionResult() method?
Last edited: