如何说GPS等待几分钟,直到返回不为零的位置



这是我的GPS代码,它返回(0,0(作为位置的latlng。如何在返回不等于的值 (0,0( 之前等待几分钟。

这是我的代码:

public class Gps implements LocationListener {
public static double lon;
public static double lat;
Location location;
@Override
public void onLocationChanged(Location location) {
this.location=location;
lon=location.getLongitude();
lat=location.getLatitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}

public String gps() {
String str_gps=null;
LocationManager lm = null;
LocationListener ll = new Gps();
lm = (LocationManager) getSystemService(G.context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
boolean gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}
gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
str_gps = "(" + Gps.lat + "," + Gps.lon + ")";
return str_gps;
}

我正在考虑使用线程,但不确定在哪里使用它。

此示例代码使用计时器来检查 GPS 的有效值。 调用 startRecording(( 获取 GPS

private Location getBestLocation() {
Location mainLocation = null;
Location gpslocation = getLocationByProvider(LocationManager.GPS_PROVIDER);
Location networkLocation =
getLocationByProvider(LocationManager.NETWORK_PROVIDER);
long old = System.currentTimeMillis() - checkInterval * 2;
boolean gpsIsOld = true;
boolean networkIsOld = true;

// if we have only one location available, the choice is easy
if (gpslocation == null) {
Log.d("gps", "No GPS Location available.");
return networkLocation;
}
if (networkLocation == null) {
Log.d("gps", "No Network Location available");
return gpslocation;
}
if (gpslocation.getTime() < old) {
if (networkLocation.getTime() < old) {
return null;
}
return networkLocation;
}

return gpslocation;
}

/**
* get the last known location from a specific provider (network/gps)
*/
@SuppressWarnings({"ResourceType"})
private Location getLocationByProvider(String provider) {
Location location = null;
LocationManager locationManager = (LocationManager) getActivity().getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
try {
if (locationManager.isProviderEnabled(provider)) {
location = locationManager.getLastKnownLocation(provider);
}
} catch (IllegalArgumentException e) {
Log.d("gps", "Cannot acces Provider " + provider);
}
return location;
}
//noinspection ResourceType
@SuppressWarnings({"ResourceType"})
public void startRecording() {
//gpsTimer.cancel();
gpsTimer = new Timer();
locationManager = (LocationManager) getActivity().getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
for (String s : locationManager.getAllProviders()) {
checkGps = false;
//startRecording();
locationManager.requestLocationUpdates(s, updateTime,
minDistance, new LocationListener() {
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
doLocationUpdate(location, true);
}
});
gps_recorder_running = true;
}
/**
if we want to access ui thread
*/

// start the gps receiver thread
timerTask = new TimerTask() {
@Override
public void run() {
Location location = getBestLocation();
if (location != null) {
double latitude = location.getLatitude();
double longtitude = location.getLongitude();
SharedPreferencesTools.savelatitude(getContext(),
getString(R.string.constant_shared_latitude), "" + latitude);
SharedPreferencesTools.saveLongtitude(getContext(),
getString(R.string.constant_shared_longtitude), "" + longtitude);
Log.i("milliSeconds", "" + date);
Log.i("location_shared", "" + location);
// 
} 
}
};
gpsTimer.schedule(timerTask, checkInterval);
}
public void doLocationUpdate(Location l, boolean force) {
Log.d("gps", "update received:" + l);
myLocation = l;
if (l == null) {
Log.d("gps", "Empty location");
if (force)
Toast.makeText(getActivity(), "Current location not available",
Toast.LENGTH_SHORT).show();
return;
}
}

最新更新