2

I am trying to implement a GPStracker in my project which I want to use to get users lat, long and post code. I am following this tutorial; http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/, however, I can't seem to get the code working and after hours trying and researching I am not sure what I should do. I am fairly new to Android so I am probably making a silly mistake.

When I follow the guide as shown on the website posted I get an error message on my code saying:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`

on the lines:

            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

and

public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }
}

I am using API 25, working with fragments and searching online this seems to be a problem with API 23 and above. Many others have posted a similar questions but when I try the answers provided for them I still can't get it to work; for example,

I tried implementing:

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return;
}

Which I assume grants the permission Android is after but it still doesn't work. If there is a new way of getting users location that you think is better please can you direct me to that.

I have also included following permissions in my project:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
SumOne
  • 817
  • 3
  • 13
  • 24
  • 2
    That tutorial is **four and a half years old**. In Android years, that is practically forever. Stick to educational materials that are more recent (ideally, less than two years old). FWIW, [here is a sample app demonstrating getting the location while also employing runtime permissions](https://github.com/commonsguy/cw-omnibus/tree/master/Location/Classic). – CommonsWare Jan 11 '17 at 23:32
  • :) true say. Will keep this is mind thanks. Thanks for the link – SumOne Jan 12 '17 at 08:55

0 Answers0