我使用此代码搜索最佳提供商:
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
criteria.setAccuracy(Criteria.ACCURACY_HIGH);
criteria.setSpeedRequired(false);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
return locationManager.getBestProvider(criteria, true);
显然,我有必要的权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
然而,它给了我一个错误
network provider does not exist, accuracy=3
奇怪的是,如果我使用
criteria.setAccuracy(Criteria.ACCURACY_FINE);//FINE, not HIGH
它运作正常。可能是什么原因?如何使用HIGH值?
答案 0 :(得分:1)
请参阅Criteria.setAccuracy:
/**
* Indicates the desired accuracy for latitude and longitude. Accuracy
* may be {@link #ACCURACY_FINE} if desired location
* is fine, else it can be {@link #ACCURACY_COARSE}.
* More accurate location may consume more power and may take longer.
*
* @throws IllegalArgumentException if accuracy is not one of the supported constants
*/
public void setAccuracy(int accuracy) {
if (accuracy < NO_REQUIREMENT || accuracy > ACCURACY_COARSE) {
throw new IllegalArgumentException("accuracy=" + accuracy);
}
if (accuracy == ACCURACY_FINE) {
mHorizontalAccuracy = ACCURACY_HIGH;
} else {
mHorizontalAccuracy = ACCURACY_LOW;
}
}
setAccuracy(ACCURACY_FINE)会将mHorizontalAccuracy分配给ACCURACY_HIGH