[英]java.lang.SecurityException: “passive” location provider requires ACCESS_FINE_LOCATION permission on API level 26
I have android application and we have recently shifted to API level 26 from 22. 我有android应用程序,我们最近已从22转移到API级别26。
I have cheked if the app has one of the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission. 我已检查该应用程序是否具有ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限之一。
One issue that I found and not able to solve is that it gives an exception like "java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission" 我发现但无法解决的一个问题是,它给出了一个类似“ java.lang.SecurityException的异常:“被动”位置提供程序需要ACCESS_FINE_LOCATION权限“
My Android manifest file has: 我的Android清单文件具有:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Also I got this kind of exception: 我也有这种例外:
Process: edu.syr.ischool.orange.indoormap3, PID: 12666
java.lang.SecurityException: "passive" location provider requires ACCESS_FINE_LOCATION permission.
at android.os.Parcel.readException(Parcel.java:2005)
at android.os.Parcel.readException(Parcel.java:1951)
at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:802)
at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1220)
at utils.CheckinUtils.checkLocation(CheckinUtils.java:100)
at utils.CheckinUtils.resume(CheckinUtils.java:247)
Also my checkinUtil --> checkLocation Method 还有我的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);
I have checked for permission of either one of them as documented on https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String) 我已经检查了https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String)上记录的其中任何一个的许可。
Still, It gives an exception for ACCESS_FINE_LOCATION but as it passes the if condition and gives the security exception so it should have the ACCESS_COARSE_LOCATION permission. 仍然,它为ACCESS_FINE_LOCATION给出了一个异常,但是当它通过if条件并给出了安全性异常时,它应该具有ACCESS_COARSE_LOCATION权限。 Then Why the application is crashing?
那么,为什么应用程序崩溃了?
As of what I understood was, You are only checking for whether permission is granted or not. 据我了解,您只在检查是否授予许可。
If your app/code not ends if the permissions were not granted, make sure to request permissions by calling ... 如果在未授予权限的情况下您的应用程序/代码未结束,请确保通过调用...来请求权限。
ActivityCompat.requestPermissions(this, permissionsStringArray,requestCodeInt);
And also while checking self permissions try using ContextCompat
instead of ActivityCompat
另外,在检查自我权限时,请尝试使用
ContextCompat
代替ActivityCompat
Solution: 解:
Instead of this line: 代替此行:
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) {
Write this: 这样写:
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
You are taking the inbuilt permission from Android, that may be the bug. 您正在获取Android的内置权限,这可能是错误。
You must take the permission from Manifest which you have mentioned there. 您必须征得清单中提到的Manifest的许可。
So instead of android.Manifest.permission.ACCESS_FINE_LOCATION
, it should be just Manifest.permission.ACCESS_FINE_LOCATION
因此,它应该不是
Manifest.permission.ACCESS_FINE_LOCATION
,而不是android.Manifest.permission.ACCESS_FINE_LOCATION
Android manifest file should be like this: 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" />
You can find more details here Request User Permissions And use your java code istead of : Location lastKnow = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
您可以在此处找到更多详细信息请求用户权限并使用Java代码代替:
Location lastKnow = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
as below 如下
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();
}
By using above code you will never face exception problems. 通过使用以上代码,您将永远不会遇到异常问题。 ** HAPPY CODING **
**快乐编码**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.