Android Studio 中 getFusedLocationClient 的警告不明确 IDE
Unclear IDE warning for getFusedLocationClient in Android Studio
我在 Android Studio 中的代码收到以下 IDE 警告:
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
我的代码如下所示。它应该设置一个 GoogleMap
对象以正常显示带有用户位置的框架,如果他们禁用位置(location == null
)或拒绝应用程序的位置权限(addOnFailureListener
).
fusedLocationClient.lastLocation.addOnFailureListener(this) {
Log.e("onRequestPermissions", "fail")
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.17, 10.45), 6F)) // Germany
}
fusedLocationClient.lastLocation.addOnSuccessListener(this) { location ->
Log.e("onRequestPermissions", "succ")
if (location == null)
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.17, 10.45), 6F)) // Germany
else
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location.latitude, location.longitude), 13F))
}
我不明白为什么调用 fusedLocationClient
(getFusedLocationClient
) 会给我这个错误消息。我认为 FusedLocationProviderClient
一直存在,我正在处理用户在 addOnFailureListener
内拒绝位置许可 的情况。那么,如果我什至不允许调用 getFusedLocationClient
.
,我怎么可能附加这个失败回调
AndroidStudio 将此警告处理为严重“错误”和全红文本,这让我的代码看起来很糟糕。
为了让 Lint 知道您“知道相应的权限检查并在您的代码中的其他地方处理它”,将 @SuppressLint("MissingPermission")
注释添加到该方法其中包含代码。
Indicates that Lint should ignore the specified warnings for the annotated element.
该错误实际上是在询问您应该显式检查是否启用了请求的权限,或者您可以通过向该代码添加 @SuppressLint 注释来禁用 lint 检查。
PermissionChecker.PERMISSION_GRANTED ==
PermissionChecker.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
))
或者
@SuppressLint("MissingPermission")
我在 Android Studio 中的代码收到以下 IDE 警告:
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
我的代码如下所示。它应该设置一个 GoogleMap
对象以正常显示带有用户位置的框架,如果他们禁用位置(location == null
)或拒绝应用程序的位置权限(addOnFailureListener
).
fusedLocationClient.lastLocation.addOnFailureListener(this) {
Log.e("onRequestPermissions", "fail")
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.17, 10.45), 6F)) // Germany
}
fusedLocationClient.lastLocation.addOnSuccessListener(this) { location ->
Log.e("onRequestPermissions", "succ")
if (location == null)
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.17, 10.45), 6F)) // Germany
else
mMap?.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location.latitude, location.longitude), 13F))
}
我不明白为什么调用 fusedLocationClient
(getFusedLocationClient
) 会给我这个错误消息。我认为 FusedLocationProviderClient
一直存在,我正在处理用户在 addOnFailureListener
内拒绝位置许可 的情况。那么,如果我什至不允许调用 getFusedLocationClient
.
AndroidStudio 将此警告处理为严重“错误”和全红文本,这让我的代码看起来很糟糕。
为了让 Lint 知道您“知道相应的权限检查并在您的代码中的其他地方处理它”,将 @SuppressLint("MissingPermission")
注释添加到该方法其中包含代码。
Indicates that Lint should ignore the specified warnings for the annotated element.
该错误实际上是在询问您应该显式检查是否启用了请求的权限,或者您可以通过向该代码添加 @SuppressLint 注释来禁用 lint 检查。
PermissionChecker.PERMISSION_GRANTED ==
PermissionChecker.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
))
或者
@SuppressLint("MissingPermission")