由于sdk版本,locationManager.getLastKnownLocation报错

使用sdk23时,locationManager.getLastKnownLocation报错

报错代码

location = locationManager.getLastKnownLocation(provider);
报错permission rejected by user 原因:23版本对permission要求更高

解决办法:stackoverflow上的:So if you are using API 23 you must check explicit permission if you would access to location service so add this:

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//DO OP WITH LOCATION SERVICE
}
I suggest you to check also if app is running on device with API >= 23 with this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//DO CHECK PE}
MISSION-

}—-
—*在代码中加入*if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//DO OP WITH LOCATIO
N SERVICE
}

你可能感兴趣的:(android总结)