等待GPS定位

时间:2013-11-03 16:54:21

标签: android gps locationlistener

我知道这已被问过几次,但我找不到一个可以让你设定等待时间的解决方案。

以下是我用来通过GPS检索用户当前位置的代码。

  /**
   * Finds users location using gps
   * satelite network.
   * 
   * @param  FragmentActivity
   * @return Location
   */
  private 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) {} 
}

现在我想实际给gpsLocation()方法30秒来获取用户位置,但我不知道如何使用wait(100)或for循环来执行此操作。

0 个答案:

没有答案