android - 请求用户权限时避免 "Screen overlay detected"错误

标签 android android-permissions

我正在开发 GoogleMap 应用程序,因此需要用户位置。当使用以下代码询问时:

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;
}

它会提示用户拒绝或允许它。当用户允许权限时,会出现“检测到屏幕覆盖”错误。

我相信会发生这种情况,因为较新的 android 不会让您在应用程序打开时更改应用程序的权限,因此您需要关闭它并在“设置”->“应用程序”中允许权限。

我的问题是,您如何对应用程序进行编程以请求用户许可,而不会遇到屏幕覆盖问题,从而使用户体验变得糟糕。

问题是,一旦提示用户拒绝/允许位置权限,然后在按下允许后,屏幕覆盖消息会立即出现。

这是处理位置的代码:

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;
}

我还添加了 onRequestPermissionResult() 方法:

@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
    }
}

最佳答案

你会讨厌这个的。
问题是您调用“showToastMessageShort(...)”。
如果您在权限请求待处理时 Toast 一条消息,您将看到您的体验。
要解决此问题,请永远不要在请求权限的代码路径中 toast 。

是的,这是废话。

更多信息:Screen overlay detected blocks Android permissions

关于android - 请求用户权限时避免 "Screen overlay detected"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39701205/

相关文章:

android - 将 EXIF 元数据写入 Android 中的图像

android - AlarmManager PendingIntent.FLAG_NO_CREATE 取消警报后返回不为空

android onLocationChanged 从未调用过

android - 授予 FileProvider 的 uri 权限会产生 SecurityException

android - 如何修复未 protected SMS BroadcastReceiver lint 警告

Android 应用程序在请求权限之前崩溃

java - 如何在 Android 中迭代非常大的字符串数组?

java - Android - 如何将 edittext 值传递给后台进程,然后在完成后启动新 Activity

java - 无法一次请求多个危险权限

android - 如何在运行时删除危险权限?