java.lang.SecurityException:“;被动的;位置提供程序需要API级别26上的访问\u精细\u位置权限
我有一个android应用程序,我们最近将API级别从22改为26 我已检查该应用程序是否具有访问\u粗略\u位置或访问\u精细\u位置权限之一 我发现的一个无法解决的问题是,它给出了一个异常,如 “java.lang.SecurityException:“被动”位置提供程序需要访问\u精细\u位置权限” 我的Android清单文件有:java.lang.SecurityException:“;被动的;位置提供程序需要API级别26上的访问\u精细\u位置权限,java,android,android-permissions,android-location,android-api-levels,Java,Android,Android Permissions,Android Location,Android Api Levels,我有一个android应用程序,我们最近将API级别从22改为26 我已检查该应用程序是否具有访问\u粗略\u位置或访问\u精细\u位置权限之一 我发现的一个无法解决的问题是,它给出了一个异常,如 “java.lang.SecurityException:“被动”位置提供程序需要访问\u精细\u位置权限” 我的Android清单文件有: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&g
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
还有我的checkinUtil-->checkLocation方法
if (ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager = (LocationManager) activity.getSystemService(serviceString);
Location lastKnow = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
我已经检查了其中任何一个的许可,记录在
尽管如此,它还是为ACCESS\u FINE\u LOCATION提供了一个异常,但当它通过if条件并提供安全异常时,它应该具有ACCESS\u rough\u LOCATION权限。那么为什么应用程序会崩溃呢?据我所知, 您只检查是否授予了权限 如果您的应用程序/代码未在未授予权限的情况下结束,请确保通过调用
ActivityCompat.requestPermissions(this, permissionsStringArray,requestCodeInt);
此外,在检查自身权限时,请尝试使用
ContextCompat
而不是ActivityCompat
解决方案:
而不是这一行:
if (ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
写下:
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
您正在从Android获取内置权限,这可能是错误所在
你必须从你提到的清单中获得许可
因此,它应该是
Manifest.permission.ACCESS\u FINE\u LOCATION
,而不是android.Manifest.permission.ACCESS\u FINE\u LOCATION
android清单文件应该是这样的:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
通过使用上述代码,您将永远不会遇到异常问题。
**HAPPY CODING**应用程序在上述日志中崩溃?您可以使用runtime吗permissions@UmangBurman,是的,应用程序在上述日志上崩溃。还有Location lastKnow=locationManager.getLastKnownLocation(locationManager.PASSIVE\u提供程序);这一行是CheckinUtils.java:100Nilesh,检查一下答案。和tryYour if表达式不正确,因为它当前要求拒绝两个权限才能执行
返回。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
try {
Location lastKnow1 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
Location lastKnow2 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location lastKnow3 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(lastKnow1 != null){
--- your code ---
} if(lastKnow2 != null){
--- your code ---
} if(lastKnow3 != null){
--- your code ---
} else {
--- Toast ---
}
} catch (Exception e) {
e.printStackTrace();
}