@nicopico getBestProvider与Criteria.ACCURACY_FINE可以返回NETWORK_PROVIDER如果没有启用GPS 我用下面的在我的应用程序代码
public void getLocation() {
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
// boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
Log.e(TAG, "getLocation fired Google Play Services are not available");
mHandler.post(new UiToastCommunicaton(context.getApplicationContext(),
context.getResources().getString(R.string.gpserv_notfound)));
}
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on
Log.e(TAG, "network location provider not enabled");
} else {
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Check provider exists then request for location
if (provider != null) {
requestLocation(locationManager, provider);
} else {
//start wifi, gps, or tell user to do so
}
}
}
public void requestLocation(LocationManager locationManager, String provider) {
Log.e(TAG, "requestLocation fired getting location now");
if (provider != null) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
//locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper());
} else {
//tell user what has happened
}
}
这个代码是实现LocationListener的 您可以编辑这行
locationManager.requestLocationUpdates(provider, 0, 0, this);
类
反映你的位置监听器的实现
为什么你甚至调用'setAccuracy()',因为显然你愿意接受任何准确性? – CommonsWare 2010-12-17 10:43:17
我想要的准确性,但在一些地方,如隧道或高层建筑物,你可以从网络获得的唯一位置... – moe 2010-12-17 15:11:25