0

We have a strong GPS signal I have an app for testing how fast a GPS lock can be established I even tested the navigation app on my phone they all get pretty much insta lock bellow 10 if I just turned on the GPS.

But with this app I just never get a lock, the wile loop just keeps going on and on and the Log never shows I let it run for 10min and nothing. Am I missing something here I know there is a possible > 10min delay but other apps have no problem that are not using lastKnownLocation but pulling an actual location.

Also the gpsLocation() method is in a static Locator class.

  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);

    Location location = Locator.gpsLocation(this);

    while(location == null)
    {
      location = Locator.gpsLocation(this);
    }

    Log.w("GPS LOCATION", "LOCATION FOUND");


  }

  /**
   * Finds users location using gps
   * satelite network.
   * 
   * @param  FragmentActivity
   * @return Location
   */
  public static Location gpsLocation(FragmentActivity context)
  {
    // Fetch LocationManager service & instance of UserLocator
    LocationManager provider = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    UserLocator     locator  = new UserLocator();

    // Attempt to get a fix on users location
    provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locator);

    // Return users location using GPS Service
    return locator.location;
  }

  /**
 * Use to listen for user location updates
 * TODO: Reimplement as a anonymous class
 */
  class UserLocator implements LocationListener
  {
    // Public accessor
    public Location location;
    public void onLocationChanged(Location location)
    {
      if(location != null)
      {
        // We have a fix on users location
        this.location = location;
      }
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider)  {}
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
  }
2
  • did you declare your permissions? Commented Nov 3, 2013 at 21:58
  • @Bob yes i do have all permissions set Commented Nov 3, 2013 at 22:02

1 Answer 1

1

I think your problem is that your location updates listener is updating the location variable inside your UserLocation class, however the variable you are checking is the one you get back from the method gpsLocation, which is null.

What you are doing is you are signing up for location updates and immediately returning the value of null and checking it over and over again, while your real location is being updated in another place.

All this is also assuming that you have declares the proper permission in your manifest file.

Try this:

Location location;

 @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
     super.onCreate(savedInstanceState);
     gpsLocation(this);
     while(location == null)
         continue;
     Log.w("GPS LOCATION", "LOCATION FOUND");
}


 public static void gpsLocation(FragmentActivity context)
 {

    LocationManager provider = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    UserLocator     locator  = new UserLocator();
    provider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locator);
 }


  class UserLocator implements LocationListener
   {
       public void onLocationChanged(Location loc)
       {
            if(loc != null)
                 location = loc;

  }
   public void onProviderDisabled(String provider) {}
   public void onProviderEnabled(String provider)  {}
   public void onStatusChanged(String provider, int status, Bundle extras) {} 
}

With this implementation, both the listener and the while loop address the same instance of Location, so once this changes from null you will get the log message.

I would however strongly discourage putting such a while(true) loop in your onCreate method, you will be blocking the main thread, and this is generally a bad thing to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible first time I ran this code it detected my location and now onChangeLocation is not throwing anything new because it has some location cached and I didn't change my location after that ?
I think that it does give you new locations. however this code will only put it in the log once, the first time it happens. If you want to really see every new location fix try to put the Log.w statement in the location listener, inside the if block in onLocationChanged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.