简体   繁体   English

Android:禁止方法调用警告

[英]Android: Suppress Warning on method call

I'm trying to do a method that checks for android permissions. 我正在尝试做一种检查android权限的方法。 But when I call it I have a Missing Permission warning. 但是,当我将其命名为“缺少权限”警告时。

Here is a simplification of my code: 这是我的代码的简化:

public void lintTestMethod(){
    if(isPermissionGranted()) {
        requiresLocationPermission();
    }
}

public boolean isPermissionGranted(){
    return true;
}

@RequiresPermission(anyOf = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION})
public void requiresLocationPermission(){

}

I tried adding: 我尝试添加:

@SuppressLint("MissingPermission")
public boolean isPermissionGranted(){
    return true;
}

But it's not working (I also tried using SuppressWarning). 但这不起作用(我也尝试过使用SuppressWarning)。

Ideally, I would like to not have to modify the lintTestMethod method. 理想情况下,我不想不必修改lintTestMethod方法。

Does anyone have an idea on how to deal with that ? 有谁知道如何处理吗?

I found a workaround to my problem by reading the lint code checking the permissions ( Code ). 通过阅读检查许可权的皮棉代码( 代码 ),我找到了解决问题的方法。 The solution is to name my method called 'isPermissionGranted' : 'check*Permission'. 解决方案是将我的方法命名为“ isPermissionGranted”:“ check * Permission”。 It's not really the solution I was looking for but it works. 这并不是我一直在寻找的解决方案,但它确实有效。 If anyone has a better way to do it I'm still interested. 如果有人有更好的方法,我仍然很感兴趣。

Here is the interesting part of the code: 这是代码中有趣的部分:

String name = node.astName().astValue();
if ((name.startsWith("check") || name.startsWith("enforce")) && name.endsWith("Permission")) {
      mChecksPermission = true;
      mDone = true;
}

You place suppression in wrong place: 您将抑制放置在错误的位置:

Single statement suppression: 单条语句抑制:

public void lintTestMethod(){
        if(isPermissionGranted()) {
            //noinspection MissingPermission
            requiresLocationPermission();
        }
    }

Method range suppression: 方法范围抑制:

@SuppressWarnings("MissingPermission")
public void lintTestMethod(){
    if(isPermissionGranted()) {
        requiresLocationPermission();
    }
}

It can be done using Android Studio quick fix menu. 可以使用Android Studio快速修复菜单来完成。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Add this in your AndroidManifest.xml file. 将此添加到您的AndroidManifest.xml文件中。

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

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