Using Criteria.ACCURACY_COARSE I get the location, but it's inaccurate. I wanted to use GPS Criteria.ACCURACY_FINE , but the onLocationChanged(Location location) method is not called.
I did some research, but could not understand why. There are many similar questions in StackOverflow, but unfortunately I could not find the one that cleared my problem.
Below is my source code:
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(); } }
Manifest file permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Why is the use of the network provider and the GPS provider not working?
I checked the GPS service on my settings and testing on Android 4.3 and Android 2.3.3.
thanks in advance
source share