错误permision ACCESS_FIND_LOCATION

时间:2017-08-24 08:54:34

标签: android

我正在使用权限ACCESS_FINE_LOCATION但是当run.error:需要ACCESS_FINE_LOCATION permission时 为什么呢?

提前感谢。

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

2 个答案:

答案 0 :(得分:1)

尝试获取位置时,

ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION都需要这两项权限  所以在清单文件中添加这两个权限,以及如果你的目标是android 6.0及以上,而不是像下面的代码那样添加这个运行时permsiion

   String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
            if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.
                    checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
                        {permission}, PERMISSION_GPS_CODE);

            }

现在处理permisiion结果

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_GPS_CODE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {


            Toast.makeText(this, "location_permission_granted ", Toast.LENGTH_SHORT).show();

        } else {

            Toast.makeText(this, "location_permission_not_granted ", Toast.LENGTH_SHORT).show();
        }
    }

}

答案 1 :(得分:1)

您是否在代码中编写了运行时权限?从Android 6.0开始,对于manifest中声明的每个权限,还必须向用户询问运行时间。

private void requestLocationPermission() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
    }
}

同样处理用户的回复:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case LOCATION_PERMISSION_CODE: {
            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}