LocationManager
在oreo
及更高版本的设备中始终返回false,即使我将其打开也仍然返回false。
如何检查oreo
和更高版本的设备中的gps是否已打开?
这里是我的代码:
Location manager;
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//this if check is always false even when gps is on
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
void onRestart(){
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//still returning false when i come back to app from settings screen after turning on gps, but it returns true in android Nougat devices perfectly
}
}
答案 0 :(得分:1)
请使用融合位置客户端
Documentation
它具有高电池效率且易于使用,并且几乎是基于位置的应用程序中的标准。
A youtube tutorial
我的一个应用程序中的一个示例:
@Override
public void onStart() {
super.onStart();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap == null) return;// GUARD
map = googleMap;
getDeviceLocationUpdates();
}
/**
* Start listening to location changes
*/
private void getDeviceLocationUpdates() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationRequest = new LocationRequest();
locationRequest.setInterval(Constants.DRIVING_TIME_INTERVAL);
locationRequest.setFastestInterval(Constants.DRIVING_TIME_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() > 0) {
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Log.i(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude());
lastLocation = location;
if (currentLocationMarker != null) {
currentLocationMarker.remove();
}
if (mapPin == null) {
mapPin = GraphicUtils.resizeImage(getActivity(), Constants.MAP_MARKER_IMAGE_NAME, 100, 100);
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
placeCurrentLocationMarker(latLng);
//move map camera
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, Constants.DEFAULT_ZOOM));
}
}
};
/**
* Places a marker with user image on the map on given location
*
* @param location GPS Location
*/
private void placeCurrentLocationMarker(LatLng location) {
Bitmap bitmap = GraphicUtils.createContactBitmap(getActivity(), BitmapFactory.decodeResource(
getActivity().getResources(),
Integer.valueOf(((MainActivity) getActivity()).currentUser.getImgUrl())));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
markerOptions.title(getString(R.string.you_are_here));
markerOptions.icon((bitmap == null) ?
BitmapDescriptorFactory.fromBitmap(mapPin) :
BitmapDescriptorFactory.fromBitmap(bitmap));
currentLocationMarker = map.addMarker(markerOptions);
}
答案 1 :(得分:0)
还有另一种可能性与该设备是否为Oreo无关,但是在通过startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
重定向的设置活动中,您可能未启用GPS提供商本身。另外,由于您说过 Oreo及以上,我认为您在运行时和清单中都已经授予android.manifest.Permission.ACCESS_FINE_LOCATION
的许可。
如果您注意到了,则有3个提供程序:GPS_PROVIDER
,NETWORK_PROVIDER
和PASSIVE_PROVIDER
。您正在使用GPS_PROVIDER
来获取您的位置,但是如果未通过设置启用该位置,则该位置可能会返回false。尝试此操作时,可能只选择了网络提供商。
这里是我在说什么的图片(来自HUAWEI-P20 Lite-Android 8.0.0 API 26):