title: Android Location and Threads
body: <p>i'm trying to make heavy use of the Location API. I have no problem whatsoever doing a simple Activity that refreshes a TextView with a LocationListener, that's fine.</p>
  
  <p>The thing is, i must put all the Location stuff in something like a Manager class that gets called by the main Activity. So i must place everything inside that class and pass say the location to the Activity. This is the Activity:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);        
      setContentView(R.layout.main);
      g = new GPSManager(getApplicationContext());
      t = new Thread(g);
      t.start();
      updateWithNewLocation(g.getLocation());
  }    
  private void updateWithNewLocation(Location location) {
      String latLongString;
      TextView myLocationText;
      myLocationText = (TextView)findViewById(R.id.loca);
      if (location != null) {
         double lat = location.getLatitude();
         double lng = location.getLongitude();
         latLongString = "Lat:" + lat + "\nLong:" + lng;
      } else {
         latLongString = "No location found";
      }
      myLocationText.setText("Your Current Position is:\n" +
      latLongString);
   }    
  </code></pre>
  
  <p>And this is the "Manager" class:</p>
  
  <pre><code>public GPSManager(Context context) {
      locationManager = (LocationManager)context.getSystemService(service); 
      String service = Context.LOCATION_SERVICE;
      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      criteria.setAltitudeRequired(false);
      criteria.setBearingRequired(false);
      criteria.setCostAllowed(true);
      criteria.setPowerRequirement(Criteria.POWER_LOW);
      provider = locationManager.getBestProvider(criteria, true); 
  }
  public Location getLocation() {
      return this.location;
  }
  private final LocationListener locationListener = new LocationListener() {
      public void onLocationChanged(Location location) {
          Manager.this.location=location;
      }
      public void onProviderDisabled(String provider){
          locationManager.removeUpdates(this);
      }
      public void onProviderEnabled(String provider){ }
      public void onStatusChanged(String provider, int status,
          Bundle extras){ }
      };
  
  @Override
  public void run() {
     Looper.prepare();
     locationManager.requestLocationUpdates(provider, 0, 0,
             locationListener);        
     location = locationManager.getLastKnownLocation(provider);
     Looper.loop();
     Looper.myLooper().quit();
  }
  private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
      locationManager.removeUpdates(locationListener);
  }
  </code></pre>
  
  <p>The problem i have is that, well, it doesn't works at all! If i put this code in the activity, no thread involved, it works great. But this way, it just says "no location found". I need to make the Manager class always listening to changes, so when i ask for the current location is updated.</p>