Android 6.0 getLastKnowLocationPermission

编程入门 行业动态 更新时间:2024-10-09 13:33:49
本文介绍了Android 6.0 getLastKnowLocationPermission的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我尝试获取Android 6.0中的最后一个已知位置时出现错误

I have a error when I'm trying to get last known location in Android 6.0

Location lastLocation =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

错误:

java.lang.SecurityException:网络"位置提供程序需要 ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限.

java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

我的清单:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

检查权限:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { try { ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, 1); } catch (Exception e) { e.printStackTrace(); } } PointDTO point = new PointDTO(); try { Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // addresses = geocoder.getFromLocation(lat, lon, 1); // // String cityName = addresses.get(0).getAddressLine(0); // // String countryName = addresses.get(0).getAddressLine(2); // address= addresses.get(0).getAddressLine(0); point.setLat(lastLocation.getLatitude()); point.setLon(lastLocation.getLongitude()); } catch (NullPointerException | SecurityException e) { e.printStackTrace(); } return point;

我做错了什么?它适用于Android Api 23-

What I did wrong? It works in Android Api 23-

UPD:

NPE:

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

SecurityException

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

推荐答案

如果为棉花糖(Android api 23或更高版本)赋予了运行时位置权限,则应该获取位置信息,

You should do get location work if runtime location permissions are given for marshmallow(android api 23 or above), check this:

这行代码

Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

这里将向您显示安全异常代码中的错误,并要求在各处使用运行时权限代码作为代码,而不必担心您可以使用此错误信息执行构建.

here will show you error in code for security exception and ask to use runtime permission code everywhere for code, no need to worry you can execute the build with this error info.

private void checkRuntimePermissions(){ if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { try { ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, 1); } catch (Exception e) { e.printStackTrace(); } }else{ getMyLocation(); } } private PointDTO getMyLocation(){ PointDTO point = new PointDTO(); try { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // addresses = geocoder.getFromLocation(lat, lon, 1); // // String cityName = addresses.get(0).getAddressLine(0); // // String countryName = addresses.get(0).getAddressLine(2); // address= addresses.get(0).getAddressLine(0); point.setLat(lastLocation.getLatitude()); point.setLon(lastLocation.getLongitude()); } catch (NullPointerException | SecurityException e) { e.printStackTrace(); } return point; } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQ_ACCESS_COARSE_LOCATION: case REQ_ACCESS_FINE_LOCATION: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! checkRuntimePermissions(); } else { //do other operation because permission not given } return; } } }

更多推荐

Android 6.0 getLastKnowLocationPermission

本文发布于:2023-11-27 11:41:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   getLastKnowLocationPermission

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!