Android位置管理器getbestlocation返回null

时间:2012-04-11 09:26:29

标签: android locationmanager android-location

我需要一些关于Android中LocationManager的一些信息/帮助的帮助。我正在使用此代码来获取用户的具体信息,具体取决于他的位置,但在某些情况下,我得到provider=null(我不确定何时,因为该异常已上传到我们的服务器,我无法重现它)。

以下是示例代码:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria crta = new Criteria(); 
crta.setAccuracy(Criteria.ACCURACY_FINE); 
crta.setAltitudeRequired(false); 
crta.setBearingRequired(false); 
crta.setCostAllowed(true); 
crta.setPowerRequirement(Criteria.POWER_LOW); 
String provider = locationManager.getBestProvider(crta, true);
Log.d("","provider : "+provider);
// String provider = LocationManager.GPS_PROVIDER; 
Location location = locationManager.getLastKnownLocation(provider); 
updateWithNewLocation(location); 
locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);

如果我的提供程序为null并且是否有办法获取当前位置并使用它,我该怎么办?

提前致谢!

1 个答案:

答案 0 :(得分:1)

尝试此代码并且不要忘记应该启用至少一个提供商

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(true);
crta.setBearingRequired(true);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW); 
String provider = locationManager.getBestProvider(crta, true);
Log.d("","provider : "+provider);
// String provider = LocationManager.GPS_PROVIDER; 
locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);
Location location = locationManager.getLastKnownLocation(provider); 
//updateWithNewLocation(location);