我使用此代码来获取位置:
final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
final Criteria locationCritera = new Criteria();
locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
locationCritera.setAltitudeRequired(false);
locationCritera.setBearingRequired(false);
locationCritera.setCostAllowed(true);
locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
final String providerName = locationManager.getBestProvider(locationCritera, true);
if (providerName != null && locationManager.isProviderEnabled(providerName)) {
locationManager.requestLocationUpdates(providerName, 20000, 100, this);
} else {
Toast.makeText(this, "GPS turned off!", Toast.LENGTH_LONG).show();
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 权限都在清单中定义。
网络和 GPS 都已打开,但我只使用参数“network”(一次)调用 onProviderEnabled。但是相同的代码在 4.0 模拟器和 Onda 4.1 Tab 上运行良好。为什么?