How to know if user allowed a Permission upon prompt [duplicate] - android

This question already has answers here:
Android marshmallow request permission?
(26 answers)
Closed 6 years ago.
When the permission Dialog pops up, it asks if I want to allow or deny a certain permission. Thing is, when I allow or deny, the action doesn't execute. How do I know if he allowed so I can execute an action upon accepting?
I tried:
int hasWriteExternalStoragePermission = ctx.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteExternalStoragePermission == PackageManager.PERMISSION_GRANTED) {
// my code
}
But it doesn't execute as it's "too late"

Try this way :
public boolean isStoragePermissionGranted()
{
if (Build.VERSION.SDK_INT >= 23)
{
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED)
{
Log.v(TAG,"Permission is granted");
return true;
}
else
{
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else
{
//permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted <23");
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED)
{
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
else
{
Log.v(TAG,"Permission denied");
}
}
This way, If the permission is granted it would come here:
Log.v(TAG,"Permission is granted");
If permission is not already granted , it would come here:
Log.v(TAG,"Permission is revoked"); and try get the permission.
And if user grants the permission in runtime :
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
if the user denies it :
Log.v(TAG,"Permission denied");
In case the version is below Marshamallow, ie below API 23, then it would come here:
Log.v(TAG,"Permission is granted <23");

Related

Running an Activity that requires permission to access the gallery

I have this activity that is supposed to access the gallery and choose an image. It run with zero errors but when pressing the "choose " button I got a Permission Denied response in accessing the gallery. What is the problem?
If you are target app marshmallow or above you need to add run time permission.
Try below code
Add Permission in your Manifest using below line
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
And use below code for get run time permission
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
Permission result callback:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
}
For more detail please prefer below link
https://developer.android.com/training/permissions/requesting
I hope this can help you!
Thank You.

How to grant WRITE_APN_SETTINGS permission in android rooted oreo

I can programmatically set, add, updated, delete APN in android 4.2.2 easily , but above android 6 I could not be able to grant WRITE_APN_SETTINGS permission using runtime permission in rooted device as a system app.
Thanks for any workaround.
Use runtime permission check if run app above 6.1 have look
int permissionCheck = ContextCompat.checkSelfPermission(YourActivity, Manifest.permission.WRITE_APN_SETTINGS);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//requesting permission
ActivityCompat.requestPermissions(YourActivity, new String[]{Manifest.permission.WRITE_APN_SETTINGS}, 1);
} else
//permission is granted
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted by user
} else
//permission denied by user
}
}
}
Hope it will help you!!

Permissions when updating an app

I am developing an Android application and I have uploaded it to Google Play. When installing the application request for the camera, storage, etc. But when doing an update, it asks me again about the permission again.
Example camera permission:
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
return false;
}
return true;
}
private void requestCameraPermission() {
Log.w(TAG, "Camera permission is not granted. Requesting permission");
final String[] permissions = new String[]{Manifest.permission.CAMERA};
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);
return;
}
Is there any way to avoid this?? Perhaps by code I can ask if the app is updating, that you do not ask for them again.

How to make user must allow certain permission to continue using apps

I am developing an apps where permission to write internal storage is crucial. When I am requesting permission for api >23, there is an option to never show the request permission dialog again. I wanted to disable this because if the user choose to deny and check never show the request dialog again, then the user will have to personally go to settings and clear the apps reference himself, which I want to avoid. I also want to keep requesting for permission if the user choose deny, but resulting in error java.lang.ArrayIndexOutOfBoundsException in grantResults[0].
To summarize my problem:
1. how to disable the "never show again check"
2. my error's fix
here's my code in requesting permission again if the user choose deny:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_WRITE_EXTERNAL_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Update();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_STORAGE);
}
break;
}
}
You can't stop the user from permanently denying the permission. When the user does an action that requires the permission, and the permission is denied, you can flash a message - a Toast or a Snackbar, for example.
try this my friend
String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
#Override
public void onRequestPermissionsResult ( int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(AccountClass.this, "permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AccountClass.this, "permission not granted", Toast.LENGTH_SHORT).show();
}
}
}

Automatically detect to show allow permission after user deny in the first time asking

I working with anroid app in Android M and need some permission that allow to access CAMERA, RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, ... so I put my permission checking when user open the app in the first time. But if user deny it, how can I detect it automatically that user is using the feature that required permission then ask them again? Or I have to put my condition in every feature that need permission.
I know when we use some feature that required permission but not allowed already it will throw the exception, so do we have any class that handle this task?
I use this logic: Every Activity extends a BaseActivity, in which there is a method that check the permissions everytime the onCreate() is called.
The method that I use is:
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
So, everytime the user launch a new Activity, the application check the permissions and display which permissions are not granted yet.
To manage the result, use onRequestPermissionsResult()
BTW, this logic will ask the user the permission even if the activity's feature doesn't require any permission.
If you want to ask the user the permissions only where the feature that requires a permission, you must check it in every activity
onRequestPermissionsResult you can check particular Permission is granted or not you can call Permission dialog again from there if particular permission not granted for Example
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
Snackbar.make(mLayout, R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
} else {
Log.i(TAG, "CAMERA permission was NOT granted.");
// Ask again for permission
}
// END_INCLUDE(permission_result)
} else if (requestCode == REQUEST_CONTACTS) {
Log.i(TAG, "Received response for contact permissions request.");
// We have requested multiple permissions for contacts, so all of them need to be
// checked.
if (PermissionUtil.verifyPermissions(grantResults)) {
// All required permissions have been granted, display contacts fragment.
Snackbar.make(mLayout, R.string.permision_available_contacts,
Snackbar.LENGTH_SHORT)
.show();
} else {
Log.i(TAG, "Contacts permissions were NOT granted.");
// Ask again for permission
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Categories

Resources