0

我需要一些关于LocationManagerAndroid 的信息/帮助的帮助。我正在使用此代码根据用户的位置获取用户的特定信息,但在某些情况下我得到了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);

当我的提供者为空并且有办法获取当前位置并使用它时,我该怎么办?

提前致谢!

4

1 回答 1

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); 
于 2012-04-11T09:59:06.773 回答