Could explain me plaese how i should explain to user permission
i work with Camera2API and i implement such snippet of code to ask permishion dinamicly
private void openCamera(int width, int height) {
setUpCameraOutputs(width, height);
CameraHelper.configureTransform(width, height, textureView, previewSize, getActivity());
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (!cameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// Show an explanation 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(
getActivity(), new String[]{android.Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST);
// MY_PERMISSIONS_REQUEST is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
according to google docks i should check if user deny permission before
there is method which return true of false
shouldShowRequestPermissionRationale();
and according google
This method returns true if the app has requested this permission previously and the user denied the request.
if i understand correctly according google comments inside this method implementation
// Show an explanation 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.
And finally for example user deny my permission before and next time when he go to my screen with camera app should create the my castom pop-up with explanation "Please agree this permishion if you want to continue" and for example user agree this time and i should recall this method again according this
// sees the explanation, try again to request the permission.
but this method
shouldShowRequestPermissionRationale();
return to me true
again, because it didn't know about the user intent to agree with permission.
Could you explain me please how to make it properly way? Maybe you have example?
Eventually i find a solution with help of @nuuneoi, thanks a lot!
And implement it like this
public void camera(View view) {
toCamera();
}
private void toCamera() {
if (!isCheckPermission()){
return;
}
if (isProcessWasFinish()) {
startActivity(new Intent(getApplicationContext(), CameraActivity.class));
overridePendingTransition(R.anim.open_next, R.anim.close_main);
} else {
startActivity(new Intent(getApplicationContext(), UserDataScreen.class));
overridePendingTransition(R.anim.open_next, R.anim.close_main);
}
}
private boolean isCheckPermission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
showMessageOKCancel("You need to allow access to Camera");
return false;
}
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST);
return false;
// MY_PERMISSIONS_REQUEST is an
// app-defined int constant. The callback method gets the
// result of the request.
}
return true;
}
private void showMessageOKCancel(String message) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", listener)
.setNegativeButton("Cancel", listener)
.create()
.show();
}
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
final int BUTTON_NEGATIVE = -2;
final int BUTTON_POSITIVE = -1;
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case BUTTON_NEGATIVE:
// int which = -2
dialog.dismiss();
break;
case BUTTON_POSITIVE:
// int which = -1
ActivityCompat.requestPermissions(
MainActivity.this, new String[]{android.Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST);
dialog.dismiss();
break;
}
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e(MY_LOG, "Camera permission Granted");
// permission was granted, yay! Do the
// contacts-related task you need to do.
toCamera();
} else {
Log.e(MY_LOG, "Camera permission Denied");
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Hey you can use some of the libraries that are great for asking user permissions on app launch.
There's a great library to help you do that in Android :
Github link for the Permission dispatcher library
You can find the usage examples of the permission dispatcher library here
You can also check these libraries :
App-Runtime-Permissions-Android
Assent
MarshmallowPermissionManager
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With