I want to update the data displayed in a TextView in some variables that are being updated every 5 seconds ... I can not use threads so I guess it goes with timer / timertask but I can not solve it ... In summary, I want to take a string that is being updated on the other hand and print it in a text view every x seconds (I only get it printed the first time, I need this textview is updated with the information of the variable that uses this) to update the location coordinates.
With this code I get the location I want (Works perfect):
//Metodos Ubicación
private void actualizarUbicacion(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
speed = location.getSpeed();
altitud = location.getAltitude();
}
}
LocationListener locListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
actualizarUbicacion(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private void miUbicacion() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PETICION_PERMISO_LOCALIZACION);
} else {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
actualizarUbicacion(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 5000,0,locListener);
}
}
After this I use threads for the time and for other methods with a socket connection with xmpp.
And to show the information of this location, inside the onCreate I use this:
miUbicacion();
TextView twLocalizacion = (TextView) findViewById(R.id.twLocalizacion);
String datos = ("Latitud= " + df.format(lat) + " Longitud= " + df.format(lng) + "\n Altura= " + altitud + " Velocidad= " + speed);
twLocalizacion.setText(datos);
I tried to do something with a timer but it did not work and as soon as I got it from the onCreate peta application, I read that you have to make a timer that runs on the UI thread but I do not know how to do it very well ... Here I leave the other topic where I saw some information to do this: link