I am reading the documentation here for the Android Nearby Permission specially for Android 12. I want to check if the user has granted access to Nearby device permission programmatically. The following check is returning true for me all the time
ContextCompat.checkSelfPermission(context,Manifest.permission.BLUETOOTH_CONNECT) ==
PackageManager.PERMISSION_GRANTED
I manually revoked the nearby permission after granting it once in the app and also confirmed by going to the app settings which says nearby permission is not granted. Is there a way to check for this runtime and show the nearby device permission(system dialog) if not granted
Related
I am making an EMM application. This application will only be installed in firm's devices only. Is there any way to grant below permissions programmatically through DevicePolicyManager.
android.permission.SYSTEM_ALERT_WINDOW (Display over other apps)
android.permission.BIND_ACCESSIBILITY_SERVICE (Accessibility service)
android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS (Ignore battery optimization)
I want to grant above permissions through DevicePolicyManager as my app is a system owner app. But, I can't grant them through devicePolicyManager.setPermissionGrantState. As I have also tried devicePolicyManager.setPermittedAccessibilityServices for granting ACCESSIBILY_SERVICE programmatically, but it also didn't work. So, is there is any way I can grant above all permissions programmatically without navigating to that screen and turn in on manually. Or Is there is a way so that user cannot be able to disable the above features anyway. Like when I enable location and other permissions through DevicePolicyManager user is unable to turn it off.
So reviewing: https://developers.google.com/android/work/requirements#4.2.-runtime-permission-grant-state-management_1
and https://developers.google.com/android/management/reference/rest/v1/enterprises.policies#permissionpolicy
seem to indicate at you can create a policy which will automatically grant permissions.
I do see:
PERMISSION_POLICY_AUTO_GRANT
Permission policy to always grant new permission requests for runtime permissions. Already granted or denied permissions are not affected by this.
and
PERMISSION_GRANT_STATE_GRANTED
Runtime permission state: The permission is granted to the app and the user cannot manage the permission through the UI.
which sounds like what you want.
This problem appeared only in android 10. I am using Nearby service for iBeacon. In my manifest I'm asking permission ACCESS_FINE_LOCATION and before start nearby service I check if my app has this permission been granted. So why i have this error and how should I properly ask permission to use nearby service? Also i don't like description in asking permission dialog. My app don't use microphone. It frightens users!
Nearby.getMessagesClient(this, new MessagesOptions.Builder()
.setPermissions(NearbyPermissions.BLE)
.build())
.subscribe(getNearbyPendingIntent(), options);
For getting the IMEI i using from this code:
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
} else {
iMEI = tm.getDeviceId();
}
But when my app is running this dialog box coming up:
The program asks to grant permission To make and manage phone calls which can scary users from using app.
And now my question is:
Why READ_PHONE_STATE permission asked "make and manage phone call"? While I have not make phone call and manage phone call in my
app.
READ_PHONE_STATE permission is listed as Dangerous permission and provides access to read phone state. It comes under the Phone permission group. If dangerous permission is asked, the system shows dialog related to its group. in your case, Phone. and That is the reason- the user is asked for "make and manage phone call" permission. This is how the permissions are asked-
https://developer.android.com/training/permissions/requesting
To make it more clear, see
https://developer.android.com/guide/topics/permissions/overview
It says -
If the device is running Android 6.0 (API level 23) and the app's
targetSdkVersion is 23 or higher, the following system behavior
applies when your app requests a dangerous permission:
If the app doesn't currently have any permissions in the permission group, the system shows the permission request dialog to the user
describing the permission group that the app wants access to. The
dialog doesn't describe the specific permission within that group.
For example, if an app requests the READ_CONTACTS permission, the
system dialog just says the app needs access to the device's
contacts. If the user grants approval, the system gives the app just
the permission it requested.
If the app has already been granted another dangerous permission in the same permission group, the system immediately grants the
permission without any interaction with the user. For example, if an
app had previously requested and been granted the READ_CONTACTS
permission, and it then requests WRITE_CONTACTS, the system
immediately grants that permission without showing the permissions
dialog to the user.
There are a lot of better ways to get a unique identifier. For example-
String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
Why READ_PHONE_STATE permission asked "make and manage phone call"?
While I have not make phone call and manage phone call in my app.
After Marshmallow we need to explicitly call the permissions which come under Dangerous permission.
READ_PHONE_STATE comes under Permission Group Called Phone
Because google got lazy. It is the same with READ_EXTERNAL_STORAGE
I keep getting a security exception when trying to get location updates in API 23. The system seems to think I don't have the appropriate permissions:
java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
However, if I am checking for user location permissions before making the call to location services and both fine and coarse location are returning PERMISSION_GRANTED :
private boolean locationPermissionsGranted(Context context){
int fineLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
int coarseLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);
return ((fineLocation == PackageManager.PERMISSION_GRANTED) || (coarseLocation == PackageManager.PERMISSION_GRANTED));
}
I set permissions at runtime by prompting the user to grant permissions with the system dialog:
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
Permissions.ACCESS_FINE_LOCATION.getPermission());
I get the exception after permissions are granted because my boolean check for whether I have access to location returns true but the system wont let me access location. I'm testing this on a OnePlus One and started to think that maybe privacy guard was screwing with things but I've set it to allow access to location so I'm not sure why the error is persisting.
Requesting Permissions at Run Time
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app.
You need to request the access location permission and handle the request response at app runtime
Android Lollipop has provided the UsageStatsManager service which allows apps to get information about device usage statistics, as long as the user of the device has granted the required permission through the Settings.
Is it possible to find out programmatically if the user has granted us the permission?
Thanks.
You can just check the size of the list returned from UsageStatsManager.queryUsageStats.
If the user has not granted permission, the list will be empty.