How to Check Grants Permissions At Run-Time

How to check Grants Permissions at Run-Time?

Nice !!

I just found my need we can check if the permission is granted by :

checkSelfPermission(Manifest.permission.READ_CONTACTS)

Request permissions if necessary

if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant

return;
}

Handle the permissions request response

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! do the
// calendar task you need to do.

} else {

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

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

How to check if permission is granted by user at runtime on Android?

Use onRequestPermissionResult, It handles the action if user press ALLOW and DENY, Just call the intent in the condition "if the user presses allow":

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 123: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If user presses allow
Toast.makeText(Main2Activity.this, "Permission granted!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
startActivity(in);
} else {
//If user presses deny
Toast.makeText(Main2Activity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}

Hope this helps.

How to check Grant Permissions at Run Time when phone is locked?

It is not possible to ask permission when the phone is locked. But you can send a Notification to the user to request permission.

On the click of Notification open an activity and get a permission from the user.

More info about Notification.https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Android M - check runtime permission - how to determine if the user checked Never ask again ?

Developer Preview 2 brings some changes to how permissions are requested by the app (see also http://developer.android.com/preview/support.html#preview2-notes).

The first dialog now looks like this:

Sample Image

There's no "Never show again" check-box (unlike developer preview 1). If the user denies the permission and if the permission is essential for the app it could present another dialog to explain the reason the app asks for that permission, e.g. like this:

Sample Image

If the user declines again the app should either shut down if it absolutely needs that permission or keep running with limited functionality. If the user reconsiders (and selects re-try), the permission is requested again. This time the prompt looks like this:

Sample Image

The second time the "Never ask again" check-box is shown. If the user denies again and the check-box is ticked nothing more should happen.
Whether or not the check-box is ticked can be determined by using Activity.shouldShowRequestPermissionRationale(String), e.g. like this:

if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)) {...

That's what the Android documentation says (https://developer.android.com/training/permissions/requesting.html):

To help find the situations where you need to provide extra
explanation, the system provides the
Activity.shouldShowRequestPermissionRationale(String) method. This
method returns true if the app has requested this permission
previously and the user denied the request. That indicates that you
should probably explain to the user why you need the permission.

If the user turned down the permission request in the past and chose
the Don't ask again option in the permission request system dialog,
this method returns false. The method also returns false if the device
policy prohibits the app from having that permission.

To know if the user denied with "never ask again" you can check again the shouldShowRequestPermissionRationale method in your onRequestPermissionsResult when the user did not grant the permission.

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_PERMISSION) {
// for each permission check if the user granted/denied them
// you may want to group the rationale in a single dialog,
// this is just an example
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
boolean showRationale = shouldShowRequestPermissionRationale( permission );
if (! showRationale) {
// user also CHECKED "never ask again"
// you can either enable some fall back,
// disable features of your app
// or open another dialog explaining
// again the permission and directing to
// the app setting
} else if (Manifest.permission.WRITE_CONTACTS.equals(permission)) {
showRationale(permission, R.string.permission_denied_contacts);
// user did NOT check "never ask again"
// this is a good place to explain the user
// why you need the permission and ask if he wants
// to accept it (the rationale)
} else if ( /* possibly check more permissions...*/ ) {
}
}
}
}
}

You can open your app setting with this code:

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);

There is no way of sending the user directly to the Authorization page.

Check dangerous permissions every time

does this mean that runtime permission dialog has to show up 3 times a day whenever my service is going to perform its work

Probably not.

it has to show up only the first time my service starts and then it checks programmatically (only) the remaining 2 times if permission was granted ?

Probably not.

The quote from the documentation means that you need to call ContextCompat.checkSelfPermission() before you try performing an operation that needs a runtime permission (dangerous permissions). This does not display any UI. It merely lets you know whether or not you hold the permission.

If you do not hold the permission, you will to request the permission from the user before you will be able to do whatever it is that you are trying to do. That needs to be performed from an activity or fragment, using requestPermissions().

In your case, before you schedule the background work (WorkManager, JobScheduler, etc.), use requestPermissions(), and only schedule that work if the user grants you your desired permission(s).

However, it is possible that the user will revoke the granted permissions. That is why your background work will need to call checkSelfPermission(). If that indicates that you do not hold the permission, you will need to raise a Notification that leads the user to your UI, where you can once again requestPermissions().

How permission can be checked at runtime without throwing SecurityException?

You can use Context.checkCallingorSelfPermission() function for this. Here is an example:

private boolean checkWriteExternalPermission()
{
String permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
int res = getContext().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}

How to check android application permissions on runtime using ContextCompat.checkSelfPermission()?

Please follow this tutorials Here is complete source code

static final int PERMISSION_ALL = 1;
String[] PERMISSIONS =
{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};

protected void runtimePermission() {
if (!hasPermission(ContactUS.this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}

public static boolean hasPermission(Context context, String... permissions) {
if (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;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_ALL:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted
//your logic here
} else {
//permission denied
}
break;

}
}

Android 11 Runtime Permissions

My issue was fixed by the system reset, but according to comments and documents please follow the below guidelines for the location permission.

Thanks Pawel for comments,

Ref: https://stackoverflow.com/a/66321942/9909365

Background location permission does not work like other permissions. It's a request to elevate location permission from foreground-only to foreground & background.

User has to consciously select "Allow all the time" in order to do that and grant background location permission. Otherwise that permission is considered denied.

You cannot even request background location unless foreground location is already granted - when system permission activity shows up it should already have option 2 or 3 selected.

See https://developer.android.com/training/location/permissions#request-background-location

Note:

When your app is using this background location please be prepared with a short video that demonstrates the location-based feature in your app that requires access to location in the background (while the app is not in use).

See https://support.google.com/googleplay/android-developer/answer/9799150



Related Topics



Leave a reply



Submit