即使在运行时请求权限后,也会出现误报lint权限错误 [英] False positive lint permission error even after asking for permission at runtime

查看:172
本文介绍了即使在运行时请求权限后,也会出现误报lint权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解android使用的运行时权限方法。对于那些正在阅读本文的人,我不是在这里问如何调用运行时权限..如果它尚未被授予,我可以显示一个权限对话框,甚至可以在片段中获得它的onRequestPermissionsResult()。

I am having hard time understanding runtime permission approach used by android. For those who are reading this, I am not asking here "how to call runtime permission".. i am able to show a permission dialog if it's not already been granted and even get it's result in onRequestPermissionsResult() in fragment.

使用此代码

 if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        FragmentCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RQ_GET_LOCATION);
        Log.e("permission", "asking permission");
        return;
    }
   Log.e("permission", "permission already granted");
   accessLocation();

获得结果 onRequestPermissionsResult

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

    switch (requestCode) {
        case RQ_GET_LOCATION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e("permission", "onRequestPermissionsResult : permission granted, access location here");
                accessLocation();
            }
            else {
                Log.e("permission", "onRequestPermissionsResult : permission denied");
            }
            break;
    }
}

中的代码accessLocation()

 private void accessLocation()
{
    Log.e("permission", "accessing location permission");
    Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        userDetail.setLat(String.valueOf(mLastLocation.getLatitude()));
        userDetail.setLat(String.valueOf(mLastLocation.getLongitude()));
    }
    else {
        if (!isLocationEnabled()) {
            mShowAlert();
        }
    }
}

到目前为止好.. .. 但是困扰我的是我的 accessLocation()方法我在线上得到误报错误

so far so good.. but what's bothering me is that inside my accessLocation() method i'm getting false positive error on line

Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

表示


调用需要用户可能拒绝的权限:代码应明确检查权限是否可用(使用 checkPermission )或显式处理潜在的 SecurityException

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException

我知道当你没有检查权限时会出现这种情况。一旦我使用 @SuppressWarnings(MissingPermission)注释方法

i know this comes when you you don't check for permission. This error goes as soon as i annotate method with @SuppressWarnings("MissingPermission")

我就不知道了这个错误我不知道当开发人员必须在班级中多次调用方法时,他们如何实际处理这些错误。
要么我必须使用 @SuppressWarnings(MissingPermission)或? (我不知道)

i don't know how developers are actually handling such errors when they have to call method at multiple times in their class. either i have to use @SuppressWarnings("MissingPermission") or ? (i don't know)

假设我在上面的第一个代码块中请求了一个权限>弹出权限对话框>用户授予权限>流程转到 onRequestPermissionsResult 我再次编写访问位置的代码,但是如果没有检查权限,这也会显示误报错误。

suppose i request a permission like i am doing in the first code block above > permission dialog pops up > user grants permission > flow goes to onRequestPermissionsResult and again i write code for accessing location but that will also show me a false positive error for not checking permission..

要么我正在做一些可怕的事情,要么我认为我误解了许可模式或者有一些lint的错误(我真的不认为这是问题)..

either i'm doing something terrible or i think i misunderstood the permission model or there is some bug with lint (i REALLY don't think this is the problem)..

我已经查看了此stackoverflow问题这个工作室错误但我对那里的答案不满意。

I've already looked into this stackoverflow question and this studio bug but i am not satisfied with the answers there.

请告诉我我做错了什么?

推荐答案

首先:你没有做错任何事。您的代码将按预期工作。

First off: You are not doing anything wrong. Your code will work just as expected.

我建议只是压制警告,因为它显然是错误的。

I would advise to just suppress the warning, as it is clearly wrong.

但是,如果要满足Lint要求,则必须将代码重构为以下模式

However, if you want to fulfill the Lint requirements, you have to refactor your code to the following schema

void doSomething(){
    if(permission granted){
        //do stuff (directly in here, do not delegate this to other methods)
    }else{
        //request permission
    }
}

void onRequestPermissionsResult(...){
    if(permission granted){
        doSomething();
    }
}

是的,这将导致双重检查,如果早先没有授予许可。是的,结果代码不好读。但警告将消失。

Yes, this will result in a double check if the permission was not granted earlier. Yes, the resulting code is bad to read. But the warning will go away.

运行时许可系统非常糟糕,我们无能为力。

The runtime permission system is horrible and there is nothing we can do about it.

这篇关于即使在运行时请求权限后,也会出现误报lint权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆