简体   繁体   English

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

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

I want to request for user's location every time the activity comes into screen (I think my only option here is activity's onResume() method) and I want to (and should) support android 6's new runtime permission model. 每当活动进入屏幕时,我想请求用户的位置(我认为我唯一的选择是活动的onResume()方法),并且我想要(并且应该)支持android 6的新运行时权限模型。

I am using permissionDispatcher library and it works perfectly. 我正在使用permissionDispatcher库,它运行完美。 The problem is as this issue suggests , if I call a function which requests a permission in the onResume() method and user denies the permission, it will stuck in an infinite loop. 问题是这个问题所暗示的 ,如果我在onResume()方法中调用一个请求许可的函数,而用户拒绝该许可,它将陷入无限循环。

But my problem is that since I want to get the user's location on every appearance of the activity, I cannot think of a different way other than using the onResume() method (and hence, getting stuck in an infinite loop if the user denies the permission). 但是我的问题是,由于我想在活动的每次出现时都获取用户的位置,因此除了使用onResume()方法外,我无法想到其他方法(因此,如果用户拒绝该操作,则会陷入无限循环允许)。

here's a sample of my code: 这是我的代码示例:

    @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();
    }

what can I do for my situation? 我该如何处理?

Until unless the permission is so necessary that we have to close the activity without the permission, I don't recommend requesting permissions in onResume(). 除非有非常必要的许可,否则我们必须在没有许可的情况下关闭活动,否则,建议您不要在onResume()中请求许可。

If you deny a permission without never ask again flag set true, as many number of times onResume() is called that many times you have to request for permissions. 如果您拒绝许可而没有再询问标志设置为true,那么调用onResume()的次数就是您必须请求许可的次数。

I do understand that if you consider all the possible cases you might want to put it in onResume(), but think that it may also annoy user in some use cases. 我确实了解,如果考虑所有可能的情况,则可能希望将其放在onResume()中,但认为在某些用例中,它还会使用户烦恼。

I suggest you to put requesting part in onStart() instead of onResume() 我建议您将请求部分放在onStart()而不是onResume()中

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

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