每隔多少秒调用一次 onLocationChanged?我想每 30 秒更新一次用户的位置,但我不知道该怎么做。
PS:我使用的是网络位置提供程序,而不是 GPS。我知道 GPS 更准确,但我关心电池消耗和室内位置。
这是我的方法,它每隔一定的时间间隔调用一次(我不知道到底有多少)..
TextView tvLong,tvLati;
Button btnGet;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLong =(TextView)findViewById(R.id.tvLong);
tvLati = (TextView)findViewById(R.id.tvLati);
btnGet = (Button)findViewById(R.id.btnGet);
btnGet.setOnClickListener(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
tvLati.setText("Location not available");
tvLong.setText("Location not available");
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this); // I dont know what this exactly DOES.
}
public void onLocationChanged(Location location) {
lat = (location.getLatitude());
lng = (location.getLongitude());
String valueLati = "Latitude: " + lat;
String valueLong = "Longitude: " + lng;
tvLati.setText(valueLati);
tvLong.setText(valueLong);
}