1
這是我的GPS代碼。並返回(0,0)作爲位置latlng。如何在返回值不等於(0,0)之前等待幾分鐘。怎麼說GPS等待幾分鐘直到返回不爲零位置
這是我的代碼:
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;
}
我想使用一個線程,但不知道在哪裏使用它。
爲什麼不使用回調或監聽器,或RxJava,或'LiveData'或事件總線,提供您的位置時的位置可用? – CommonsWare
如何使用監聽器? – sonia
請參閱[本示例應用程序](https://github.com/commonsguy/cw-omnibus/tree/v8.7/Location/Classic),它對位置變化做出反應並顯示天氣預報(注意:預測可能僅適用於在美國)。 – CommonsWare