0

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!

Saumil
  • 2,521
  • 5
  • 32
  • 54
  • What is your `targetSdkVersion`? What version of Android are you testing on? Do you have your `` elements in the proper spot in the manifest? https://commonsware.com/blog/2015/08/31/hey-where-did-my-permission-go.html – CommonsWare Feb 05 '16 at 18:49
  • @CommonsWare My SDK version is 23. I have showed you the spot where I have kept the permissions in my edited answer. – Saumil Feb 05 '16 at 18:58
  • @CommonsWare I referred the link to the blog and I was able to solve my problem after downgrading the targetSdkVersion to 22. Thanks! – Saumil Feb 05 '16 at 19:03

0 Answers0