Dan
Dan

Reputation: 574

Android security exception for permission listed in manifest file

The user of the app I am developing is required to share the location during the registration process. Clicking the "Locate me" button should get the GPS coordinates ( or whatever is provided, I don't know yet ).

When the button is clicked, I get this error message:

java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.

Which, from what I understand, has to do with listing the permission in the manifest file. But the permission is there.

The threads with similar issues I found suggest uninstalling the app from the device I am testing it on, cleaning and rebuilding the project. I did all that, but the problem persists. I must be missing something else, possibly very obvious. Any ideas?

Thanks!

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest package = "com.iskillu.iskillu"
      xmlns:android = "http://schemas.android.com/apk/res/android">

    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="23"/>

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


    <application
        android:allowBackup = "true"
        android:icon = "@mipmap/ic_launcher"
        android:label = "@string/app_name"
        android:supportsRtl = "true"
        android:theme = "@style/AppTheme">
        <activity
            android:name = ".MainActivity"
            android:label = "@string/app_name"
            android:theme = "@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name = "android.intent.action.MAIN" />

                <category android:name = "android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<!-- 
            ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
-->
        <meta-data
            android:name = "com.google.android.gms.version"
            android:value = "@integer/google_play_services_version" />

        <activity
            android:name = ".JoinActivity"
            android:label = "@string/title_activity_join"
            android:theme = "@style/AppTheme.NoActionBar">
        </activity>

        <activity
            android:name = ".LoginActivity"
            android:label = "@string/title_activity_login"
            android:theme = "@style/AppTheme.NoActionBar">
        </activity>

        <activity android:name = ".RegistrationSuccessfulActivity">
            <intent-filter>
                <action android:name = "android.intent.action.VIEW" />

                <category android:name = "android.intent.category.DEFAULT" />
                <category android:name = "android.intent.category.BROWSABLE" />

                <data android:scheme = "http" />
                <data android:scheme = "https" />
                <data android:host = "www.iskillu.com" />
                <data android:host = "iskillu.com" />
            </intent-filter>
        </activity>
        <activity android:name = ".presentation.PresentationActivity">
        </activity>
    </application>
</manifest>

This is the method which throws the exception when executed:

    public void getGpsLocation ( View view )
        {
            LocationManager locationManager = ( LocationManager )        this.getSystemService( Context.LOCATION_SERVICE );

            boolean isProviderEnabled = locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER);

            if ( ! locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER) )
            {
                Toast.makeText ( this.getApplicationContext (), R.string.enable_gps, Toast.LENGTH_LONG ).show () ;
            }
            else
            {
                try {
                    Location myLocation =         locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if (myLocation != null)
                    {
                        Log.d("TAG", "Not null");
                    }
                    else
                    {
                        Log.d("TAG", "NULL");
                    }
                }
                catch (SecurityException se) {
                    Log.d("TAG", "SE CAUGHT");
                    se.printStackTrace();
                }
            }
        }

Upvotes: 4

Views: 7807

Answers (1)

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Starting from Android M you will need to request the permission at run time Requesting Permissions at Run Time

Upvotes: 3

Related Questions