应用程序故意不给予位置许可后退出

时间:2019-04-18 11:42:33

标签: android geolocation android-permissions

当权限请求屏幕弹出时我故意不执行任何操作时,会发生此问题,该应用将在几秒钟后崩溃。

从另一个活动(LoginActivity)转换为MapsActivity后,权限请求会弹出。

LoginActivity

if statement to check the validity
    if valid
        Intent intent = new Intent(LoginActivity.this,MapActivity.class);
        startActivity(intent);
        finish();

MapActivity

...    
public void onMapReady(GoogleMap map) {
    mMap = map;
    getLocationPermission();
    updateLocationUI();
    getDeviceLocation();
}

private void getLocationPermission() {
    if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mLocationPermissionGranted = true;
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
    }
}

2 个答案:

答案 0 :(得分:0)

我正在以这种方式检查许可,并且可以正常工作:-

if (checkLocationPermission()) {
                    Log.e("Tag", "Camera and External storage Permission's are allowed");
                } else {
                    requestLocationPermission();
                } 

这是方法checkLocationPermission:-

private boolean checkLocationPermission() {
        int result = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED) {
            return true;
        } else {
            return false;
        }
    }

这是位置请求方法:-

private void requestLocationPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale
                        (this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, R.style.DialogTheme);
            alertDialog.setMessage("You Have To Give Permission From Your Device Setting To go in Setting Please Click on Settings Button");
            alertDialog.setCancelable(false);
            alertDialog.setPositiveButton("Go To Settings", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
            });
            alertDialog.show();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1012);
        }
    }

最后是onRequestPermissionsResult:-

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, R.style.DialogTheme);
            alertDialog.setMessage("You Have To Give Permission From Your Device Setting To go in Setting Please Click on Settings Button");
            alertDialog.setCancelable(false);
            alertDialog.setPositiveButton("Go To Settings", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
            });
            alertDialog.show();
        }else{
          // do your stuff in case accept permission
        }

    }

答案 1 :(得分:0)

刚刚意识到如何使用简单的解决方案来解决这个问题... 在我的原始代码中,我已经有onRequestPermissionResult()了。

MapActivity

private boolean mLocationPermissionGranted;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    mLocationPermissionGranted = false;
    switch (requestCode) {
        case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                mLocationPermissionGranted = true;
            }
        }
    }
    updateLocationUI();
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
...
getLocationPermission();
    if(mLocationPermissionGranted){
        updateLocationUI();
        getDeviceLocation();

    }
}

现在不再崩溃:)