Android 对于启用gps的检入oreo+;,Location Manager始终返回false;安卓系统中的设备
当我检查gps是否打开时,Android 对于启用gps的检入oreo+;,Location Manager始终返回false;安卓系统中的设备,android,google-play-services,android-8.0-oreo,android-location,android-fusedlocation,Android,Google Play Services,Android 8.0 Oreo,Android Location,Android Fusedlocation,当我检查gps是否打开时,LocationManager在oreo及以上设备中总是返回false,即使我打开它,它仍然返回false。 如何检查oreo及以上设备中的gps是否开启 这是我的密码: Location manager; manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (!manager.isProviderEnabled(LocationManager.GPS_PROVID
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
}
}
请使用融合位置客户端
它的电池效率高,使用非常方便,几乎是基于位置的应用程序的标准。
我的一个应用程序的示例:
@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);
}
@覆盖
public void onStart(){
super.onStart();
fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(getActivity());
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
if(googleMap==null)返回;//GUARD
地图=谷歌地图;
getDeviceLocationUpdates();
}
/**
*开始收听位置更改
*/
私有void getDeviceLocationUpdate(){
if(ActivityCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(getActivity(),Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
locationRequest=新的locationRequest();
locationRequest.setInterval(常数、行驶时间和间隔);
locationRequest.SetFastTestInterval(常数、行驶时间间隔);
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
fusedLocationProviderClient.requestLocationUpdates(locationRequest、locationCallback、Looper.myLooper());
}
LocationCallback LocationCallback=新LocationCallback(){
@凌驾
public void onLocationResult(LocationResult LocationResult){
List locationList=locationResult.getLocations();
如果(locationList.size()>0){
//列表中的最后一个位置是最新位置
Location=locationList.get(locationList.size()-1);
Log.i(标记“Location:+Location.getLatitude()+”+Location.getLatitude());
lastLocation=位置;
如果(currentLocationMarker!=null){
currentLocationMarker.remove();
}
如果(mapPin==null){
mapPin=GraphicUtils.resizeImage(getActivity(),Constants.MAP\u MARKER\u IMAGE\u NAME,100100);
}
LatLng LatLng=新LatLng(location.getLatitude(),location.getLongitude());
placeCurrentLocationMarker(latLng);
//移动地图照相机
moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,Constants.DEFAULT_ZOOM));
}
}
};
/**
*在给定位置的地图上放置带有用户图像的标记
*
*@param位置GPS位置
*/
专用空位放置CurrentLocationMarker(板条位置){
位图位图=GraphicUtils.createContactBitmap(getActivity(),BitmapFactory.decodeResource(
getActivity().getResources(),
Integer.valueOf(((MainActivity)getActivity()).currentUser.getImgUrl());
MarkerOptions MarkerOptions=新MarkerOptions();
标记选项。位置(位置);
markerOptions.title(getString(R.string.you\u here));
markerOptions.icon((位图==null)?
BitmapDescriptorFactory.fromBitmap(mapPin):
BitmapDescriptorFactory.fromBitmap(位图));
currentLocationMarker=map.addMarker(markerOptions);
}
另一种可能与设备是否为Oreo无关,但在通过startActivity重定向的设置活动中(新意图(设置、操作、位置、源设置))代码>您可能未启用GPS提供程序本身。另外,既然你说了Oreo及以上版本,我想你已经在运行时和清单中授予了android.manifest.permission.ACCESS\u FINE\u LOCATION
如果您注意到,有3个提供商:GPS\u提供商
,网络\u提供商
和被动\u提供商
。您正在使用GPS\u提供程序
获取您的位置,但如果未通过设置启用,则可能返回false。尝试此操作时,可能只选择了网络提供商
下面是我正在谈论的图片(来自华为P20 Lite-Android 8.0.0 API 26):
我已经试过了,并且试着获取最后一个已知的位置。我的gps已经打开了,但有几次没有,这就是我的问题所在,如何检查oreo及以上设备中是否有gps。检查gps是否在您的设备上正常工作。使用谷歌地图应用程序进行检查。我编辑了答案,并添加了一个来自生产应用程序的示例,效果非常好。@MarkKazakov我很久以前就使用过这个fusedLocationProvider,您不理解这个问题,问题是我需要自动打开用户gps,或者至少检查其是否打开,您是否尝试过在gps关闭的情况下使用代码?试试看,让我知道。如果gps关闭,您的代码将无法获取位置。我想你已经在gps上运行你的应用程序了,这可能是一个缺陷。@Maximus我之前正在检查gps。您可以在这里看到如何实现这一点-您是否从开发人员控制台启用了适当的api?