"gps"位置提供者需要android 6.0的ACCESS_FINE_LOCATION权限 [英] "gps" location provider requires ACCESS_FINE_LOCATION permission for android 6.0

查看:170
本文介绍了"gps"位置提供者需要android 6.0的ACCESS_FINE_LOCATION权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android清单文件中,我为gps添加了以下访问权限的权限

In the android manifest file I have added the below permissions for gps to access the location

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

我正在使用位置管理器类"感染以下位置更新

I am fecthing Location Updates as below using Location Manager Class

 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
 private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
 locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES, 
                    this
                );

但是上面一行给出了一个异常说明

But the above line is giving an exception stating

"gps" location provider requires ACCESS_FINE_LOCATION permission

这只发生在targetSDKVersion 23(即android 6.0)上,并且我也在android 5.1中进行了测试,效果很好.

This thing only happens with the targetSDKVersion 23(i.e with android 6.0) and i have also tested in android 5.1 which works fine.

请帮助!.谢谢.

推荐答案

从6.0开始,某些权限被视为危险"(FINE_LOCATION是其中之一).

Since 6.0 some permissions are considered as "dangerous" (FINE_LOCATION is one of them).

要保护用户,必须在运行时对其进行授权,以便用户知道其是否与他的行为有关.

To protect the user, they have to be authorized at runtime, so the user know if it's related to his action.

要执行此操作:

 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

它将显示一个对话框,用户可以在其中选择是否授权您的应用使用位置或不使用位置.

It will show a DialogBox in which user will choose wether he autorize your app to use location or not.

然后使用此功能获取用户答案:

Then get the user answer by using this function :

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        return;
        }
            // other 'case' lines to check for other
            // permissions this app might request
    }
}

如果用户接受一次,则您的应用程序将记住它,而您不再需要发送此对话框.请注意,如果用户决定以后可以禁用它.然后,在请求位置之前,您必须测试是否仍授予该权限:

If the user accept it once, then your app will remember it and you won't need to send this DialogBox anymore. Note that the user could disable it later if he decided to. Then before requesting the location, you would have to test if the permission is still granted :

public boolean checkLocationPermission()
{
    String permission = "android.permission.ACCESS_FINE_LOCATION";
    int res = this.checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);
}

所有内容都在Android文档中进行了说明: http://developer.android.com/training/permissions/requesting. html

It's all explained on Android documentation : http://developer.android.com/training/permissions/requesting.html

这篇关于"gps"位置提供者需要android 6.0的ACCESS_FINE_LOCATION权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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