位置管理器删除更新权限 [英] Location Manager remove updates permission
问题描述
我正在使用android studio,compileSdkVersion是23 因为我正在使用下面的代码
I am using android studio and compileSdkVersion is 23 in that i am using below code
if(locationManager != null){
locationManager.removeUpdates(GPSListener.this);
}
要停止gps更新,其中GPS侦听器是实现LocationListener的类.
to stop gps update where GPS Listener is a class which implements LocationListener.
但是在removeUpdates行中,我低于棉绒警告
but in removeUpdates line i am getting below lint warning
通话需要许可,但可能会被用户拒绝:代码应 明确检查权限是否可用(带有
checkPermission
)或处理潜在的SecurityException
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with
checkPermission
) or handle a potentialSecurityException
我没有得到上面代码中的问题. 是否需要在清单文件中添加任何额外的权限?.
I am not getting what is the issue in the above code. Any extra permission need to be added in manifest file?.
致谢.
推荐答案
自SDK 23开始,您应该/需要在调用位置API功能之前检查许可权.这是一个如何做的例子:
Since SDK 23, you should/need to check the permission before you call Location API functionality. Here is an example of how to do it:
if (locationManager != null) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.removeUpdates(GPSListener.this);
}
}
有一个checkSelfPermission()
,用于检查您"(此应用程序)是否具有正确的权限.还有checkPermission()
,用于检查另一个进程是否具有正确的权限.
There is checkSelfPermission()
, which is to check if 'you' (this app) has the correct permissions. There is also checkPermission()
, which is to check if another process has the correct permissions.
注释
- 除了执行此运行时检查外,还需要在AndroidManifest中要求相关权限.
- 如果您的targetSdk是< 23,您应该改用
ContextCompat.checkSelfPermission()
(感谢JerryBrady)
- next to doing this runtime check, it is still also necessary to require the relevant permissions in the AndroidManifest.
- if your targetSdk is < 23, you should use
ContextCompat.checkSelfPermission()
instead (thanks to JerryBrady)
这篇关于位置管理器删除更新权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!