“位置提供者需要ACCESS_FINE_LOCATION权限"即使我使用运行时请求权限 [英] "location provider requires ACCESS_FINE_LOCATION permission" even if I use runtime request permission

查看:595
本文介绍了“位置提供者需要ACCESS_FINE_LOCATION权限"即使我使用运行时请求权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这太奇怪了.我们都知道,如果<uses-sdk android:targetSdkVersion="23"/>设置为23+,则除了在清单中声明<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />外,还必须要求运行时权限.

This is so weird. We all know that if <uses-sdk android:targetSdkVersion="23"/> is set to 23+, you must ask for runtime permissions besides declaring <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in the manifest.

多年来一直没有问题.现在,问题在于,当我将targetSdkVersion设置为"26"时,LocationManager不再起作用!并抛出此错误:

This has been working with no problem for years. Now, The problem is that when I am setting targetSdkVersion to "26", the LocationManager is not working anymore! and throws this error:

"gps" location provider requires ACCESS_FINE_LOCATION permission

我在应用程序中唯一更改的是targetsdkversion从23变为26,它破坏了应用程序!有什么想法吗?

The only thing that I change in my app is the targetsdkversion from 23 to 26 and it breaks the app! Any ideas?

推荐答案

好,我可以解决问题,如果有人遇到类似问题,我也会在这里进行解释.我应该补充一点,尽管问题已得到解决,但感觉就像是Android上的错误或缺少文档.

Ok, I could fix the problem and I'll explain it here also in case someone else is facing a similar problem. I should add that while the problem is now fixed, it feels like a bug on Android or lack of documentation.

由于一些内部项目原因,我正在请求以下位置权限:

Because of some internal project reasons, I was requesting for the location permission like this:

PackageInfo packageInfo = pm.getPackageInfo(_activity.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = null;
if (packageInfo != null)
{
    requestedPermissions = packageInfo.requestedPermissions;

    if (requestedPermissions.length > 0)
    {
        List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
        _requestedPermissionsArrayList = new ArrayList<String>();
        _requestedPermissionsArrayList.addAll(requestedPermissionsList);
    }
}

for(int i=0; i < _requestedPermissionsArrayList.size(); i++)
{
    if(_requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_FINE_LOCATION) || // api level 1
            _requestedPermissionsArrayList.get(i).equals(Manifest.permission.ACCESS_COARSE_LOCATION) // api level 1
        )
    {
        isFound = true;
        ActivityCompat.requestPermissions(_activity, new String[]{
                _requestedPermissionsArrayList.get(i)
        }, ExConsts.MY_PERMISSIONS_REQUEST);
        break;
    }
}

使用此设置,清单中的权限顺序很重要!当我有:

With this setup, the order of permissions in the manifest mattered! When I had:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

它不起作用,但是当我切换他们的订单时,它起作用了.

it didn't work but when I switched their order, it did work.

最后,我如何解决该问题?像下面一样,我提到了两个权限名称

Finally, how did I fix the problem? like below, I mentioned both permission names

ActivityCompat.requestPermissions(_activity, new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
}

现在,无论清单中的权限顺序如何,它始终可以正常工作.欢呼.

Now, no matter the order of permissions in the manifest, it always worked fine. cheers.

这篇关于“位置提供者需要ACCESS_FINE_LOCATION权限"即使我使用运行时请求权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆