Daniyal
Daniyal

Reputation: 37

How to get coordinates from GPS in Android?

This is not working:

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

After this code, it goes to catch statement. Another issue is that the solving statement is giving me error. A red line under it.

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Hint for the above error is that in the Android Studio:

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

This is my whole code to get gps coordinates from Android phone.

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {

            Toast.makeText(getApplicationContext(), "1111",  Toast.LENGTH_LONG).show();
            // no network provider is enabled
        } else {
            this.canGetLocation = true;

            Toast.makeText(getApplicationContext(), "2222",  Toast.LENGTH_LONG).show();
            // First get location from Network Provider
            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();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

Upvotes: 3

Views: 8822

Answers (3)

Michele Lacorte
Michele Lacorte

Reputation: 5363

In according to Requesting Permission :

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app.

There are 2 difference:

System permissions are divided into two categories, normal and dangerous:

Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.

Dangerous permissions can give the app access to the user's confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.

So if you are using API 23 you must check explicit permission if you would access to location service so add this:

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        || checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  //DO OP WITH LOCATION SERVICE
}

I suggest you to check also if app is running on device with API >= 23 with this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
  //DO CHECK PERMISSION
}

Upvotes: 6

mhdjazmati
mhdjazmati

Reputation: 4232

please include your manifest code too ,have you added those permissions ?

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

Upvotes: 0

Alireza Hosseini
Alireza Hosseini

Reputation: 970

You should add following line to your AndroidManifest.xml:

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

Upvotes: 0

Related Questions