作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Android 6.0 中尝试获取最后已知位置时出现错误
Location lastLocation =locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
错误:
java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
我的 list :
<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-
更新:
NPE:
ActivityCompat.requestPermissions(getActivity(), new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
1);
安全异常
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
最佳答案
如果为 marshmallow(android api 23 或更高版本)提供了运行时位置权限,您应该获取位置工作,请检查:
这行代码
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
这里会显示代码中的安全异常错误,并要求在所有代码中使用运行时权限代码,不用担心,您可以使用此错误信息执行构建。
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;
}
}
}
关于安卓 6.0 getLastKnowLocationPermission,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40783020/
在 Android 6.0 中尝试获取最后已知位置时出现错误 Location lastLocation =locationManager.getLastKnownLocation(LocationM
我是一名优秀的程序员,十分优秀!