0

I was designing an android application which just prints the current location coordinates (latitude and longitude) using GPS.

Here is my code:

public class GPSActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new MyLocationListener();
        try {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        } catch (SecurityException e) {
            System.out.println("GPS consent not given. Exception of type : " + e.toString());
        }
    }

    public class MyLocationListener implements LocationListener{
        @Override
        public void onLocationChanged(Location location) {
            String message = String.format("Longitude : " + location.getLongitude() + " Latitude : "
                    + location.getLatitude());
            System.out.println("Longitude : " + location.getLongitude());
            System.out.println("Latitude : " + location.getLatitude());
            Toast.makeText(GPSActivity.this, message, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    }
}

And here is my manifest file:

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Recording Settings"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".ParametricActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RecordingActivity" >
        </activity>
        <activity android:name=".SettingsActivity" >
        </activity>
        <activity android:name=".GPSActivity" >
        </activity>
    </application>
</manifest>

Even though I have added the required permissions, it throws an exception of type SecurityException when run.

07-04 15:27:10.510 10220-10220/stackquestions.mitacsinternship W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 07-04 15:27:11.769 10220-10220/stackquestions.mitacsinternship I/System.out: GPS consent not given. Exception of type : java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission. 07-04 15:27:11.935 10220-10357/stackquestions.mitacsinternship D/OpenGLRenderer: endAllActiveAnimators on 0xb8413ff8 (ListPopupWindow$DropDownListView) with handle 0xb8471c30

I have followed numerous stack posts to find a solution and have tried some of them. I am still unable to resolve this. Any help on this is greatly appreciated.

I have been using Moto G3 (Android Lollipop 6.0.1 API 23) and a htc phone of API level 19 (4.4.2) for development. The problem persists in both version.

Aditya
  • 1,172
  • 11
  • 32
  • 1
    Possible duplicate of [Android permission doesn't work even if I have declared it](http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it) – Mike M. Jul 04 '16 at 21:36
  • I read that answer already. I don't want to drop the target version below 23. This is only a part of the application that I am developing. Much of the code requires API level 23. – Aditya Jul 04 '16 at 21:39
  • That's not what the accepted answer recommends at all. It simply mentions that as "a temporary workaround". You need to request the necessary permissions at runtime. – Mike M. Jul 04 '16 at 21:40
  • How do I add the necessary permissions at runtime? I will also check for a stack answer on that already. – Aditya Jul 04 '16 at 21:42
  • https://developer.android.com/training/permissions/requesting.html – CommonsWare Jul 04 '16 at 21:43
  • Thanks. I was checking out that link. – Aditya Jul 04 '16 at 21:43

0 Answers0