我知道这已被问过几次,但我找不到允许您设置等待多长时间的解决方案。
这是我用来通过 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 循环来做到这一点。