title: Android, Get GPS location when clicking a button
body: <p>I am trying to learn how to use GPS service in Android and for that, I am developing a program that show the actual gps location when the user press a button.</p>
  
  <p>But whenever I press it, I get a 0.0...</p>
  
  <p>I think this is the piece of code that is not working fine: </p>
  
  <pre><code>LocationManager mlocManager=null;
      LocationListener mlocListener;
      mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      mlocListener = new MyLocationListener();
      mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
  
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Log.d(LOGTAG,"onCLickButton");
                  firstCoord.setText("");
              Log.d(LOGTAG,Double.toString(MyLocationListener.latitude));
              Log.d(LOGTAG, "AAAAAAAA");
              if(MyLocationListener.latitude&gt;0)
                  {
                      coordLat1 = MyLocationListener.latitude;
                      coordLon1 = MyLocationListener.longitude;
                      firstCoord.setText(Double.toString(coordLat1) + ' ' + Double.toString(coordLon1));
                  }
                  else
                  {
                      AlertDialog.Builder alert = new AlertDialog.Builder(home_screen.this);
                      alert.setTitle("Wait");
                      alert.setMessage("GPS en progress, wait.");
                      alert.setPositiveButton("OK", null);
                      alert.show();
                  }
          }
      });
  </code></pre>
  
  <p>The first log after onClickbutton, shows 0.0 but If I use google maps app, it shows my mobile phone where it is. So problem I think is I am not getting coordinates.</p>
  
  <p>This is my MyLocationListener class:
      package com.gps.distance;</p>
  
  <pre><code>import android.location.Location;
  import android.location.LocationListener;
  import android.os.Bundle;
  import android.util.Log;
  
  public class MyLocationListener implements LocationListener {
  
  private static final String LOGTAG = "LogsAndroid";
  public static double latitude;
  public static double longitude;
  
  @Override
  public void onLocationChanged(Location loc)
  {
      loc.getLatitude();
      loc.getLongitude();
      latitude=loc.getLatitude();
      longitude=loc.getLongitude();
      Log.d(LOGTAG, "TAKE POS");
  }
  
  @Override
  public void onProviderDisabled(String provider)
  {
      //print "Currently GPS is Disabled";
      Log.d(LOGTAG, "GPS DISABLED");
  }
  @Override
  public void onProviderEnabled(String provider)
  {
      //print "GPS got Enabled";
      Log.d(LOGTAG, "GPS ENABLED");
  }
  @Override
  public void onStatusChanged(String provider, int status, Bundle extras)
  {
  }
  }
  </code></pre>
  
  <p>onLocationChanged is never used. If I Disable or enable GPS, logs appear but onLocationChanged not... Never.</p>