I'm trying to get the user's location using Network as well as GPS. This is how my flow goes -
- Get LocationManager
- Check if Network_Provider is enabled
- If enabled, check for permissions, acquire permissions and get the location using network.
- If not, check for GPS_Provider, if not enabled, enable GPS from settings activity else acquire permissions and get the location using GPS.
The problem - When I'm trying to request the permissions, the app goes into force close mode. Perhaps, I'm asking permissions in the wrong place? Please throw some light.
Here's the code for same -
private void getLocationMgr() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.v(TAG, "Network enabled? " + isNetworkEnabled);
if (isNetworkEnabled) {
getLocation(locationManager, LocationManager.NETWORK_PROVIDER);
} else {
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled == false){ // go to GPS settings}
else {
getLocation(locationManager, LocationManager.GPS_PROVIDER);
}
}
}
private void getLocation(final LocationManager locationManager, final String provider) {
// Getting permissions from user for location access
if (ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERM_FINE_LOCATION);
}
locationManager.requestSingleUpdate(provider, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.v(TAG, "Provider: " + provider);
Log.v(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude());
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
if (addresses.size() > 0) {
Log.v(TAG, "City : " + addresses.get(0).getLocality());
Log.v(TAG, "Country: " + addresses.get(0).getCountryCode());
setCityExecute(addresses.get(0).getLocality(), addresses.get(0).getCountryCode());
} else {
Log.v(TAG, "Unable to get city");
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}, null);
}
Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission. at android.os.Parcel.readException(Parcel.java:1684) at android.os.Parcel.readException(Parcel.java:1637) at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:614) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:887) at android.location.LocationManager.requestSingleUpdate(LocationManager.java:695) at com.pavanbawdane.weatherinformation.MainActivity.getLocation(MainActivity.java:140) at com.pavanbawdane.weatherinformation.MainActivity.getLocationMgr(MainActivity.java:99) at com.pavanbawdane.weatherinformation.MainActivity.onCreate(MainActivity.java:81)