I am developing a GoogleMap application and thus require the users location. When asking this with the following code:
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// all good, do my thing.
}else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, this.MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);
return false;
}
it will prompt the user to deny or allow it. When user Allows the permission then the "Screen overlay detected" error shows up.
I believe this happen because the newer androids will not let you change the permissions of an app when the app is opened and thus you need to close it an allow to permissions in the Setting->App.
My question is how would you program the app to ask user the permission without getting into trouble with the screen overlay and thus making the UX horrible.
The problem is that once the users is prompted to DENY/ALLOW the location permission then after ALLOW is pressed the screen overlay message appears immediately.
Here is the code which handles the location:
public boolean requestCurrentLocation(float zoomLevel){
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mZoomLevel = zoomLevel;
// Check if gps is enabled
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean gpsEnabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!gpsEnabled){
Log.d("GPS_TAG", "Gps not enabled");
showToastMessageShort(getResources().getString(R.string.cannot_get_location));
Intent gpsOptionsIntent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
return false;
}
try{
Log.d("GPS_TAG", "Calling FusedLocationApi request location updates");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}catch(IllegalStateException ie){
Log.d("GPS_TAG", "Error requesting FusedLocationApi locationUpdates: " + ie);
return false;
}
}else{
Log.d("GPS_TAG", "Location access not granted, asking for grant");
showToastMessageShort(getResources().getString(R.string.cannot_get_location));
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, this.MY_PERMISSIONS_REQUEST_ACCESS_LOCATION);
return false;
}
return true;
}
I also added the onRequestPermissionResult() method:
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
requestCurrentLocation(ZOOM_LEVEL_BUILDING);
} else {
Log.e("GPS_", "Cannot get gps location data, permission not granted!");
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
You're going to hate this.
The problem is your call to "showToastMessageShort(...)".
You will see what you experienced if you Toast a message while a permissions request is pending.
To fix this, don't ever Toast in the code path of requesting permissions.
Yes, this is BS.
For more info: Screen overlay detected blocks Android permissions
Related
In the program below I ask for SMS permission before sending a message.
At first App execution, when the message request is confirmed, the permission are checked and, if missing, a pop up requesting the user to allow the app is issued.
When this pop up is issued, the app is moved in background.
I found many topics about this issue but no fixes for my problem. E.g. the nohistory entry is missing in the manifest.
private void checkForSmsPermission() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
return;
} else {
// Permission already granted. Enable the SMS button.
return;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_SEND_SMS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
public void send_command (String text){
checkForSmsPermission();
//Get the SmsManager instance and call the sendTextMessage method to send message
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(recipient_number, null, text, null,null);
}
This is happening because you are calling following method anyways.
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(recipient_number, null, text, null,null);
You should only call them if you are sure that you have the permission else your app will crash.
Its always recommended to call these method on success of permission validation. but in your case it will get call whether the user provided the permission or not.
I have an Android application that sends GPS location to a DB.
I tested it today and found that the location manager needs permission to call requestLocationUpdates.
I want to set permission on allowed and I don't want to ask the device or the user for permission.
Now, the code requires to add some code to ask for permission?
/**
* Enable location services
*/
public void connect() {
Log.d(TAG, ".connect() entered");
// Check if location provider is enabled
String locationProvider = LocationManager.NETWORK_PROVIDER;
if (!locationManager.isProviderEnabled(locationProvider)) {
Log.d(TAG, "Location provider not enabled.");
app.setCurrentLocation(null);
return;
}
// register for location updates, if location provider and permission are available
String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null) {
locationManager.requestLocationUpdates(bestProvider, Constants.LOCATION_MIN_TIME, Constants.LOCATION_MIN_DISTANCE, this);
app.setCurrentLocation(locationManager.getLastKnownLocation(locationProvider));
}
}
/**
* Disable location services
*/
public void disconnect() {
Log.d(TAG, ".disconnect() entered");
String locationProvider = LocationManager.NETWORK_PROVIDER;
if (locationManager.isProviderEnabled(locationProvider)) {
locationManager.removeUpdates(this);
}
}
I have added to manifest too.
PS: This permission is newly required because I have been testing the app days ago on the same device ( Android 4.4) and it worked well.
Is it possible that some default permission on Android has changed since 2017/08/21? If so how can I change it?
You have to ask for permission on run time from Marshmallow version of android. Go through this link
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. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.
We use following code to check for location permission:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
You will have to write a method to check whether permission is given by user. If not then ask user for permission.
Constants.LOCATION_PERMISSION_REQUEST is just requestcode to match when got callback in onRequestpermission result
public static boolean checkLocationPermission(Activity activity){
if(ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, Constants.LOCATION_PERMISSION_REQUEST);
return false;
}
return true;
}
As the user allows, you will get callback on overridden method "onRequestPermissionsResult()"
It is the best library to check permissions
com.github.fccaikai:AndroidPermissionX:1.0.0
Example code:
final PermissionCompat.Builder builder = new PermissionCompat.Builder(activity);
builder.addPermissions(new String[]{
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
});
builder.addPermissionRationale(activity.getString(R.string.permission_rationale));
builder.addRequestPermissionsCallBack(new OnRequestPermissionsCallBack() {
#Override
public void onGrant() {
Log.d(TAG, "Grant");
}
#Override
public void onDenied(String permission) {
Log.e(TAG, "Denied");
}
});
builder.build().request();
Lastly I ran into problem that, I can't scan for beacons because of lack of ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
I tried to fix it by using code found here, but it actually help me partially.
When this view appears
I click allow. After that I doesn't get this java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results error anymore, but I can't still see my beacons and when I open settings view the location is turned off like on picture bellow.
When I turn on location by finger everything works ok, so I can see my beacons and app works as it should. And here is the question is these some kind of bug or I missed something to turn on location from code behind after access to device location is turned on?
For developing I use Nexus 5x with android 7.1.1.
EDITED:
Code is copied from tutorial linked above, the fragment with button which starts beacon scanner:
public void onBleClicked(View view)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
}
});
builder.show();
}
BleManager bleManager = new BleManager(this);
bleManager.tryToTurnOnBle();
}
Fragment of manifest where permissions are declared:
<!-- app only for phones -->
<uses-feature android:name="android.hardware.telephony"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
The bluetooth permissions are apparently in library.
What I found right now is fact that there is similar question to mine here.
But this solution with redirecting user to location option screen is not seems to be clean one for me.
Location can be determined by two ways:
1.Using NETWORK_PROVIDER
2.Using GPS_PROVIDER
NETWORK_PROVIDER: It determines the location of the users using cell towers,wifi access points. It is commonly used for determining location inside the rooms or buildings. Here the GPS coordinates are not able to be obtained.
You can specify either
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
or
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
in order to get location using the NETWORK_PROVIDER.
GPS_PROVIDER:
It determines the location of the users using satellites. For this, the GPS coordinates are obtained and used for positioning. The GPS receiver in the smartphone receives the signals from satellites. These signals are processed and precise locations are determined.It works better in outdoors – direct sky/satellite views and communication occurs.
You need specify the permission
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
in order to use location from GPS_PROVIDER.
Fine locations:
It gives better and accurate locations. So, that I recommend you to use this to get your beacon locations. It gives permission for using both GPS_PROVIDER and NETWORK_PROVIDER or GPS_PROVIDER only for determining the position.
Coarse locations:
It provides less accurate locations.It gives permission for using NETWORK_PROVIDER only for determining the position.
Now, come to the implementation.
- Declare the above said two permissions in the AnroidManifest.xml file:
- In the java part, do the following:
Request the permission if it not granted yet:
private void requestPermission(Activity activity) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE}, MainActivity.PERMISSION_REQUEST_CODE_LOCATION);
}
When the above method is called, a dialog asking permission will appear. On selecting Allow or Deny, the below callback gets triggered.
In the onRequestPermissionsResult
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST_CODE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
//Location permission is given. Check if the providers are available and start location updates.
if (isGpsProviderEnabled && isNetworkProviderEnabled) {
startLocationUpdates();
} else {
Log.d(TAG, "GPS and Network providers are disabled");
}
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
boolean should = ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
if (should) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MainActivity.PERMISSION_REQUEST_CODE_LOCATION);
} else {
promptSettings();
}
}
}
}
In the promptSettings() method, let the user to enable location from the Settings screen.
private void promptSettings() {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(getResources().getString(R.string.unable_to_find_location));
builder.setMessage(getResources().getString(R.string.message_denied_location_permission));
builder.setCancelable(false);
builder.setPositiveButton(getResources().getString(R.string.go_to_settings), (dialog, which) -> {
dialog.dismiss();
builder = null;
if (!checkPermission(MainActivity.this)) {
goToSettings();
}
});
builder.show();
}
In the check permissions method:
public boolean checkPermission(Context context) {
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
return result == PackageManager.PERMISSION_GRANTED;
}
The goToSettings() allows the user to go to Settings screen:
private void goToSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 1);
}
Note: You need to give the below permissions in the manifest to scan the beacons. I hope you are doing that, if not please do it.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
As of Android Marshmallow (6.0), Location must be turned on in settings for apps to scan for Bluetooth LE devices including beacons. This requirement is in addition to the requirement that apps get dynamic permissions. You can see code below to query for location services being turned on and to prompt the user to turn it on if needed.
private void verifyLocationServices() {
final LocationManager manager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This app requires that location services be enabled. Please enable location in settings.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
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);
}
}
I'm developing an app with connectivity which connects to a Bluetooth device with SDK 23 as compile with. I'm having problems with requesting multiple permissions for Bluetooth. This is what I have done so far:
#Override
public void onStart() {
super.onStart();
if (D)
Log.e(TAG, "++ ON START ++");
if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this,
Manifest.permission.BLUETOOTH)
!= PackageManager.PERMISSION_GRANTED) {
} else {
ActivityCompat.requestPermissions(MyBlueToothClientActivity.this,
new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN},
REQUEST_ENABLE_BT);
}
if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this,
Manifest.permission.BLUETOOTH)
!= PackageManager.PERMISSION_GRANTED) {
} else {
ActivityCompat.requestPermissions(MyBlueToothClientActivity.this,
new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN},
REQUEST_CONNECT_DEVICE_INSECURE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ENABLE_BT: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
if (CommonData.mChatService == null)
setupChat();
Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show();
}
return;
}
case REQUEST_CONNECT_DEVICE_INSECURE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_CONNECT_DEVICE_INSECURE);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
if (CommonData.mChatService == null)
setupChat();
Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Although I'm able to get the dialogue box for requesting enabling the Bluetooth, I don't get the second permission, i.e. to connect to a device. In the logcat, I get:
01-01 06:41:24.334 25473-25473 E/BluetoothChat: ++ ON START ++
01-01 06:41:24.344 25473-25473 W/Activity: Can reqeust only one set of permissions at a time
And since I'm not able to connect to the device, I just get stuck here. And this code works fine on Android version up to Lollipop, just causes problem on the Marshmallow version.
BLUETOOTH and BLUETOOTH_ADMIN are normal permissions and are therefore they are automatically granted. Only permissions in the table of dangerous permissions need to requested at runtime.
However, as mentioned in the Android 6.0 changes: Access to Hardware Identifier:
To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:
WifiManager.getScanResults()
BluetoothDevice.ACTION_FOUND
BluetoothLeScanner.startScan()
If you're using any of those methods, you'll need to request at least ACCESS_COARSE_LOCATION at runtime (as it is a dangerous permission).