我有android应用程序,我们最近已从22更改为API级别26。
我很高兴该应用是否具有ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限之一。
我发现但无法解决的一个问题是它给出了类似的异常 “ java.lang.SecurityException:“被动”位置提供者需要ACCESS_FINE_LOCATION权限”
我的Android清单文件具有:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我也有这种例外情况:
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)
也是我的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_FINE_LOCATION给出了一个异常,但是当它通过if条件并给出了安全性异常时,它应该具有ACCESS_COARSE_LOCATION权限。那么,为什么应用程序崩溃了?
答案 0 :(得分:0)
据我了解, 您仅在检查是否授予许可。
如果在未授予权限的情况下您的应用程序/代码未结束,请确保通过调用...来请求权限。
ActivityCompat.requestPermissions(this, permissionsStringArray,requestCodeInt);
另外,在检查自我权限时,请尝试使用ContextCompat
代替ActivityCompat
答案 1 :(得分:0)
解决方案:
代替此行:
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获取内置权限,这可能是错误。
您必须获得清单中提到的清单的许可。
因此,它应该只是android.Manifest.permission.ACCESS_FINE_LOCATION
而不是Manifest.permission.ACCESS_FINE_LOCATION
答案 2 :(得分:0)
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" />
您可以在这里Request User Permissions中找到更多详细信息
并使用您的Java代码代替:
Location lastKnow = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
如下
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();
}
使用上述代码,您将永远不会遇到异常问题。 **快乐编码**