繁体   English   中英

如何在运行时使用Android 6及更高版本中的PermissionDispatcher库在`onResume()`中请求权限

[英]how to request for permission in `onResume()` in runtime using permissionDispatcher library in android 6 and above

每当活动进入屏幕时,我想请求用户的位置(我认为我唯一的选择是活动的onResume()方法),并且我想要(并且应该)支持android 6的新运行时权限模型。

我正在使用permissionDispatcher库,它运行完美。 问题是这个问题所暗示的 ,如果我在onResume()方法中调用一个请求许可的函数,而用户拒绝该许可,它将陷入无限循环。

但是我的问题是,由于我想在活动的每次出现时都获取用户的位置,因此除了使用onResume()方法外,我无法想到其他方法(因此,如果用户拒绝该操作,则会陷入无限循环允许)。

这是我的代码示例:

    @NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
    void startLocationUpdate() {
        mLocationLoadingDialog.show();
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @OnShowRationale(Manifest.permission.ACCESS_FINE_LOCATION)
    void showRationalForFineLocation(final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setMessage("this app needs your permission to get your location")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        request.proceed();
                    }
                })
                .setNegativeButton("no way", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        request.cancel();
                        exitApp();
                    }
                })
                .setCancelable(false)
                .show();
    }

    @OnPermissionDenied(Manifest.permission.ACCESS_FINE_LOCATION)
    void showDeniedForFINELOCATION() {
        exitApp();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        RequestSelectorActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);

    }

    private void exitApp() {
        new AlertDialog.Builder(this)
                .setCancelable(false)
                .setMessage("you didn't allow us to know where you are, so the application will exit")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        moveTaskToBack(true);
                        android.os.Process.killProcess(android.os.Process.myPid());
                        System.exit(1);
                    }
                }).show();
    }

我该如何处理?

除非有非常必要的许可,否则我们必须在没有许可的情况下关闭活动,否则,建议您不要在onResume()中请求许可。

如果您拒绝许可而没有再询问标志设置为true,那么调用onResume()的次数就是您必须请求许可的次数。

我确实了解,如果考虑所有可能的情况,则可能希望将其放在onResume()中,但认为在某些用例中,它还会使用户烦恼。

我建议您将请求部分放在onStart()而不是onResume()中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM