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

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

问题描述

我通过

获取用户的位置

 位置位置= LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);

code的这条线是一个方法内调用此方法我做检查Android的运行时间权限之前。只有在权限是可以从用户的话,我调用此方法。 code为正常使用。

问题是,Android的工作室还表示此线路上不承认,我已经调用此函数之前已经选中了错误。

 呼叫需要许可可由用户被拒绝:code应该明确地检查,看是否许可可用(用`checkPermission`)或明确处理潜在的`SecurityException`

现在我怎么去掉这个警告?我已经检查了权限,不希望再次检查只是为了消除此警告。我已经尝试添加@燮pressWarnings(),但不知道确切的字符串传递到这一点。但它显然是不推荐的 @燮pressWarnings({所有})的作品。

我如何删除这个警告?

编辑1:这是我的确切code -

 私人无效checkPermissions(){
    如果(ContextCompat.checkSelfPermission(背景下,Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED)
        的getLocation(); //调用的方法,如果我有权限
}私人无效的getLocation(){
    // Android的工作室列出了警告在此行。
    地点= LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
}

但是,如果我把这个权限检查里面的getLocation()方法则警告消失。 @燮pressWarnings({MissingPermission})没有工作。

编辑2:我发现,只有这样,才能燮preSS警告是 -

添加在那个特定的一段code的顶部此评论 -

  // noinspection的ResourceType

或添加此 -

  @燮pressWarnings({的ResourceType})


解决方案

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

  @覆盖
@燮pressWarnings({MissingPermission})
公共无效onRequestPermissionsResult(INT申请code,@NonNull的String []的权限,@NonNull INT [] grantResults){
    如果(LOCATION_REQUEST_ code ==请求code){
        如果(grantResults.length大于0&放大器;&放大器; grantResults [0] == PackageManager.PERMISSION_GRANTED){            lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
       }
}

编辑:

正确的皮棉规则的 MissingPermission ,但似乎有一个bug,它放错地方的一些人。

所以,请尽量的 @燮pressWarnings({的ResourceType})的了。

I'm getting the user's location through

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

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.

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`

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.

How do I remove this warning?

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

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

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

or adding this -

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


       }
}

EDITED:

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

So, please try @SuppressWarnings({"ResourceType"}), too.

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

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