简体   繁体   中英

Android: Suppress Warning on method call

I'm trying to do a method that checks for android permissions. 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).

Ideally, I would like to not have to modify the lintTestMethod method.

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'. 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.

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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