Android - Update Wireless TextView

0

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

    
asked by Chuflitas 06.04.2018 в 13:31
source

1 answer

0

You can use a CountDownTimer infinite. The only thing you have to cancel when you pause your Activity. For example:

private final int timeInMillis = 5000;
private CountDownTimer actualizador;

In your onCreate or create a method that is called once do the following:

actualizador = new CountDownTimer(timeInMillis, timeInMillis) {

      public void onTick(long millisUntilFinished) { }

      public void onFinish() {
        // Aqui actualizas la data que quieras cada 5 segundos
        this.start(); // Esto hace que se repita cada 5 segundos.
      }
};

You must overwrite the lifecycle of onPause() u onResume() to stop or summarize the timer to update respectively.

Example:

@Override
public void onPause(){
   super.onPause();
   if(this.actualizador != null)
      this.actualizador.cancel();
}

@Override
public void onResume(){
   super.onResume();
   if(this.actualizador != null)
      this.actualizador.start()
}
    
answered by 17.05.2018 в 02:24