在要求用户延迟时,避免“检测到屏幕覆盖”错误

时间:2016-09-26 11:07:17

标签: 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;
}

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

我相信这是因为当应用程序打开时,较新的机器人不会让您更改应用程序的权限,因此您需要在设置 - >应用程序中关闭它以允许权限。

我的问题是你如何编写应用程序来询问用户的权限,而不会遇到屏幕覆盖问题,从而使用户体验非常糟糕。

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

以下是处理位置的代码:

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

1 个答案:

答案 0 :(得分:1)

你会讨厌这个。
问题是你打电话给" showToastMessageShort(...)"。
如果您在权限请求待处理时向Toast传递消息,您将看到所体验的内容 要解决这个问题,请不要在请求权限的代码路径中使用Toast。

是的,这是BS。

了解更多信息:Screen overlay detected blocks Android permissions