title: How to get coordinates from GPS in Android?
body: <p>This is not working:</p>
  
  <pre><code>isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  </code></pre>
  
  <p>After this code, it goes to catch statement.
  Another issue is that the solving statement is giving me error. A red line under it.</p>
  
  <pre><code>locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  </code></pre>
  
  <p>Hint for the above error is that in the Android Studio:</p>
  
  <blockquote>
    <p>Call requires permission which may be rejected by user: code should
    explicitly check to see if permission is available (with
    <code>checkPermission</code>) or explicitly handle a potential
    <code>SecurityException</code></p>
  </blockquote>
  
  <p>This is my whole code to get gps coordinates from Android phone.</p>
  
  <pre><code>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 &amp;&amp; !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;
  }
  </code></pre>