title: GPS connection delay android
body: <p>So I'm basically sitting with an app project, that has to scan data and give the location of let's say west coast of US while I'm sitting on the east coast.</p>
  
  <p>The main problem here is that when I start the app it asks for permission to turn on "Location/GPS". If this is done and I start the scan too quickly it'll get my location for 0.0LAT and 0.0LONG which will give me an insane distance between me and the other location and is stuck in an endless while loop (probably a rookie thing that I have no clue how to exit, see the while loop futher down).</p>
  
  <ul>
  <li>I belive I've searched almost everywhere but I can't seem to find a solution, my best answer is to make a sleep thread timer for it, and make it try to get correct location after 20sec?</li>
  <li>Another thing I could think of is using onStatusChange although I'm not entirely sure.</li>
  </ul>
  
  <p>Any ideas out there?</p>
  
  <pre><code>while(mLat.equals("0.0") &amp;&amp; mLon.equals("0.0")) {
  
              mLat = String.valueOf(gpsHelper.getLatitude());
              mLon = String.valueOf(gpsHelper.getLongitude());
  
              Location.distanceBetween(Double.valueOf(mLat), 
              Double.valueOf(mLon), Double.valueOf(lat), Double.valueOf(lon), dist);
              System.out.println("lat: " + lat + "\nlong: " + lon + "\nmLat: " + mLat + "\nmLong: " + mLon + "\n" + "\nDist: " + Arrays.toString(dist));
          } 
  </code></pre>
  
  <p>So here is the GPSHelper:</p>
  
  <pre><code>public final class GPSHelper implements LocationListener {
  
  //**************************************************************************/
  // VARIABLES
  //**************************************************************************/
  //region Variables
  
  private String TAG = "GPSHelper";
  
  // Context using GPS
  private final Context mContext;
  
  // Flag for GPS status
  private boolean canGetLocation = false;
  
  // Properties
  private Location location;
  private double latitude;
  private double longitude;
  private double speed;
  
  // The minimum Distance to change Updates in meters
  private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meter
  // The minimum time between updates in milliseconds
  private static final long MIN_TIME_BW_UPDATES = 1; // 1 millisecond
  
  // Declaring a Location Manager
  private LocationManager locationManager;
  
  //endregion
  
  //**************************************************************************/
  // PROPERTIES
  /***************************************************************************/
  //region Properties
  
  public Location getLocation() {
      return location;
  }
  
  private void setLocation(Location location) {
      this.location = location;
  }
  
  //endregion
  
  //**************************************************************************/
  // CONSTRUCTOR
  /***************************************************************************/
  //region Constructor
  
  public GPSHelper(Context context) {
      this.mContext = context;
      connectToGPS();
  }
  
  //endregion
  
  //**************************************************************************/
  // FUNCTIONS
  //**************************************************************************/
  //region Functions
  
  //***************************************************/
  // Connect til GPS
  //***************************************************/
  public void connectToGPS() {
      try {
          locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  
          // Flag for GPS turned on
          boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
          Log.i(TAG, "Enabled: " + isGPSEnabled);
  
          // Is GPS turned on
          if (isGPSEnabled) {
              this.canGetLocation = true;
  
              locationManager.requestLocationUpdates(
                      LocationManager.GPS_PROVIDER,
                      MIN_TIME_BW_UPDATES,
                      MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  
              updateLocation();
          } else {
              this.canGetLocation = false;
              this.setLocation(null);
          }
      } catch (SecurityException e) {
          e.printStackTrace();
      }
  
  
  }
  
  //***************************************************/
  // Update location
  //***************************************************/
  private void updateLocation() throws SecurityException {
      // Get location
      if (locationManager != null) {
          if (getLocation() != null) {
              latitude = getLocation().getLatitude();
              longitude = getLocation().getLongitude();
              speed = getLocation().getSpeed();
          }
      }
  }
  
  //***************************************************/
  // Stop use of GPS
  //***************************************************/
  public void disconnectFromGPS() {
      if (locationManager != null) {
          locationManager.removeUpdates(GPSHelper.this);
      }
  }
  
  //***************************************************/
  // Get latitude/Breddegrad
  //***************************************************/
  public double getLatitude() {
      updateLocation();
  
      if (getLocation() != null) {
          return latitude;
      } else {
          return 0;
      }
  }
  
  //***************************************************/
  // Get longitude/Længdegrad
  //***************************************************/
  public double getLongitude() {
      updateLocation();
  
      if (getLocation() != null) {
          return longitude;
      } else {
          return 0;
      }
  }
  
  //***************************************************/
  // Get speed
  //***************************************************/
  public double getSpeed() {
      updateLocation();
  
      if (getLocation() != null) {
          double tempSpeed = speed / 3.6;
          //DecimalFormat  df = new DecimalFormat("#");
          //tempSpeed = Double.valueOf(df.format(tempSpeed));
          //tempSpeed = Math.round(tempSpeed);
          return tempSpeed;
      } else {
          return 0;
      }
  }
  
  //***************************************************/
  // Check for connection to satellites
  //***************************************************/
  public boolean canGetLocation() {
  
      return this.canGetLocation;
  
  }
  
  //***************************************************/
  // Ask user to turn on GPS
  //***************************************************/
  public void showSettingsAlert() {
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  
      // Set title
      alertDialog.setTitle(mContext.getString(R.string.gps_helper_gps_status));
      // Set message
      alertDialog.setMessage(mContext.getString(R.string.gps_helper_gps_is_not_enabled));
  
      // "Ja" button
      alertDialog.setPositiveButton(mContext.getString(R.string.yes),
              (dialog, which) -&gt; {
                  Intent intent = new Intent(
                          Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                  mContext.startActivity(intent);
              });
  
      // "Nej" button
      alertDialog.setNegativeButton(mContext.getString(R.string.no),
              (dialog, which) -&gt; dialog.cancel());
  
      // Show message
      alertDialog.show();
  }
  
  //endregion
  
  //**************************************************************************/
  // EVENTS
  //**************************************************************************/
  //region Events
  
  @Override
  public void onLocationChanged(Location location) {
      this.setLocation(location);
  
      latitude = location.getLatitude();
      longitude = location.getLongitude();
      speed = location.getSpeed();
  }
  
  @Override
  public void onStatusChanged(String s, int i, Bundle bundle) {
  
  }
  
  @Override
  public void onProviderEnabled(String s) {
  
  }
  
  @Override
  public void onProviderDisabled(String s) {
  
  }
  //endregion}
  </code></pre>