I'm accessing the location services by calling the below 2 functions,
private Location getBestLocation() {
Location gpslocation = getLocationByProvider(LocationManager.GPS_PROVIDER);
Location networkLocation =
getLocationByProvider(LocationManager.NETWORK_PROVIDER);
// if we have only one location available, the choice is easy
if (gpslocation == null) {
Log.d("", "No GPS Location available.");
return networkLocation;
}
if (networkLocation == null) {
Log.d("", "No Network Location available");
return gpslocation;
}
// a locationupdate is considered 'old' if its older than the configured
// update interval. this means, we didn't get a
// update from this provider since the last check
// both are old return the newer of those two
if (gpslocation.getTime() > networkLocation.getTime()) {
Log.d("", "Both are old, returning gps(newer)");
return gpslocation;
} else {
Log.d("", "Both are old, returning network(newer)");
return networkLocation;
}
}
/**
* get the last known location from a specific provider (network/gps)
*/
private Location getLocationByProvider(String provider) {
Location location = null;
/*if (!isProviderSupported(provider)) {
return null;
}*/
LocationManager locationManager = (LocationManager) getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
try {
if (locationManager.isProviderEnabled(provider)) {
try {
location = locationManager.getLastKnownLocation(provider);
}catch(SecurityException e){
info.setText("No permission: "+e);
}
}
} catch (IllegalArgumentException e) {
Log.d("", "Cannot acces Provider " + provider);
}
return location;
}
I have a TextView
variable and I'm setting it by,
info.setText(getBestLocation()+"");
I'm getting the Exception as below,
java.lang.SecurityException: Provider network requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission
Yes I have defined the permission in the AndroidManifest file as,
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Yes, I have tried to Invalidate Caches/Restart my Android Studio.
Any suggestions for this problem?
EDIT: Here is My AndroidManifest file,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saumil.meetup">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
....
</application>
Thanks in advance!