使用Criteria.ACCURACY_COARSE
我得到位置,但它不准确。我想使用 GPS Criteria.ACCURACY_FINE
,但onLocationChanged(Location location)
没有调用该方法。
我做了一些研究,但不知道为什么。
StackOverflow 上有很多类似的问题,但不幸的是我找不到一个可以解决我的问题的问题。
下面是我的源代码:
private void locationInformationSettings(){
Criteria criteria= new Criteria();;
criteria.setAccuracy(Criteria.ACCURACY_FINE); //using Criteria.ACCURACY_COARSE works
//criteria.setPowerRequirement(Criteria.POWER_LOW);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String providername = locationManager.getBestProvider(criteria, true);
myLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
providername, //LocationManager.GPS_PROVIDER,
Utils.MINIMUM_TIME_BETWEEN_UPDATES,
Utils.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
myLocationListener
);
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
locLongitude = location.getLongitude();
locLatitude = location.getLatitude();
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
locLongitude, locLatitude);
Toast.makeText(RecordActivity.this, message, Toast.LENGTH_LONG).show();
Log.e(TAG, "Location information received:(" + locLongitude + "," + locLatitude +")");
locationManager.removeUpdates(myLocationListener);
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(RecordActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(RecordActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(RecordActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
清单文件权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
为什么使用网络提供商有效而 GPS 提供商无效?
我已经在我的设备设置中检查了 GPS 服务,并在 Android 4.3 和 Android 2.3.3 上进行了测试。
提前致谢