Android Studio - 删除安全异常警告 [英] Android Studio - remove Security Exception warning

查看:79
本文介绍了Android Studio - 删除安全异常警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过

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

这行代码在一个方法中,在调用这个方法之前,我会检查 Android 运行时权限.只有当用户可以获得权限时,我才会调用此方法.代码运行良好.

This line of code is inside a method and before calling this method I do check for Android run time permissions. Only if the permission is available from the user then I call this method. Code is working perfectly.

问题是 Android Studio 仍然在这一行显示错误,无法识别我在调用此函数之前已经检查过.

The problem is that Android Studio still shows an error on this line not recognising that I've already checked before calling this function.

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() 但不知道传递给它的确切字符串.@SuppressWarnings({"all"}) 有效,但显然不推荐使用.

Now how do I remove this warning? I've already checked for permissions and don't want to check again just to remove this warning. I've tried adding @SuppressWarnings() but don't know the exact String to pass into this. @SuppressWarnings({"all"}) works but it is obviously not recommended.

如何删除此警告?

编辑 1 : 这是我的确切代码 -

EDIT 1 : This is my exact code -

private void checkPermissions() {
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED)
        getLocation();  //Method called if I have permission
}

private void getLocation() {
    //Android studio shows warning at this line.
    Location location = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
}

但是如果我将权限检查放在 getLocation() 方法中,那么警告就会消失.@SuppressWarnings({"MissingPermission"}) 不起作用.

But if I put the permission check inside getLocation() method then the warning disappears. @SuppressWarnings({"MissingPermission"}) did not work.

编辑 2: 我发现抑制警告的唯一方法是 -

EDIT 2: I've discovered that the only way to suppress the warning are -

在该特定代码段之上添加此注释 -

Adding this comment on top of that particular piece of code -

//noinspection ResourceType

或添加此 -

@SuppressWarnings({"ResourceType"})

推荐答案

您正在寻找(已更新以提高清晰度):

You are looking for (updated to improve clarity):

@Override
@SuppressWarnings({"MissingPermission"})
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (LOCATION_REQUEST_CODE == requestCode) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);


       }
}

正确的 lint 规则是 MissingPermission,但似乎有一个错误让某些人误将其放置.

The correct lint rule is MissingPermission, but there seems to be a bug that misplace it to some people.

所以,也请试试@SuppressWarnings({"ResourceType"}).

这篇关于Android Studio - 删除安全异常警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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