简体   繁体   English

java.lang.SecurityException:“被动”位置提供者需要API级别26的ACCESS_FINE_LOCATION权限

[英]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.

相关问题 由以下原因引起:java.lang.SecurityException:“被动”位置提供者需要ACCESS_FINE_LOCATION权限 - Caused by: java.lang.SecurityException: “passive” location provider requires ACCESS_FINE_LOCATION permission java.lang.SecurityException:提供者gps需要ACCESS_FINE_LOCATION权限 - java.lang.SecurityException: Provider gps requires ACCESS_FINE_LOCATION permission 错误:java.lang.SecurityException:我的位置需要权限 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION - ERROR:java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION java.lang.SecurityException: UID 10243 在 Android 10 中没有粗略/精细定位权限 - java.lang.SecurityException: UID 10243 does not have Coarse/Fine Location permission in Android 10 Android GoogleMaps在某些设备上不工作,显示java.lang.SecurityException:需要INSTALL_LOCATION_PROVIDER权限 - Android GoogleMaps not working on some devices shows java.lang.SecurityException: need INSTALL_LOCATION_PROVIDER permission 权限违规 - uid=10293 不允许 getConfiguredNetworks,原因=java.lang.SecurityException:UID 10293 没有位置权限 - Permission violation - getConfiguredNetworks not allowed for uid=10293, reason=java.lang.SecurityException: UID 10293 has no location permission 启动定位服务时出现java.lang.SecurityException - java.lang.SecurityException when starting location service 无法在Android中获得ACCESS_FINE_LOCATION权限 - Can't get ACCESS_FINE_LOCATION permission in android Android权限ACCESS_FINE_LOCATION始终被拒绝 - Android Permission ACCESS_FINE_LOCATION is always denied java.lang.SecurityException:权限被拒绝 - java.lang.SecurityException: Permission denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM