Android: Stop the cycle, visualize the result and return to the cycle

2

I have the following code, as you can see I am permanently cycling the program, this is because I need to be monitoring the changes that occur, I need to see the Toast messages, but as the program never stops me its visualization, try to use Thread.sleep and sleep without success, I need to stop the momentary execution of this cycle, realize the message display and then resume the cycle, someone knows how, or another way to do this THANK YOU!

 public void monitoreo(){

    try{

       estado_comparacion= estado_actual;
       while (comparacion()) {
           ResultSet rs = hilo.ConsultaSql("select * from alumno where id_alumno ='" + ids_alumnos[1] + "'");
           if (rs.next()) {
               estado_comparacion = rs.getString("estado");
           }
       }
    }
    catch (Exception e){
        e.printStackTrace();
        Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
    }
}
public boolean comparacion() {

    if (estado_actual.equals(estado_comparacion)){
        Toast.makeText(this, "No hay cambios", Toast.LENGTH_SHORT).show();
        return true;
    }else{
        estado_actual = estado_comparacion;
        Toast.makeText(this, "Hay cambios", Toast.LENGTH_SHORT).show();
        a=0;
        return  true;
    }
}

Resolved thanks to: Hictus.

Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();

public void startTimer() {
    //set a new Timer
    timer = new Timer();
    //initialize the TimerTask's job
    initializeTimerTask();
    //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
    timer.schedule(timerTask, 2000, 5000); //
}

public void initializeTimerTask() {

    timerTask = new TimerTask() {

        public void run() {
            //use a handler to run a toast that shows the current timestamp
            handler.post(new Runnable() {
                public void run() {
                    //get the current timeStamp
                    //show the toast

                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(getApplicationContext(), "Voy a correr monitoreo espera", duration);
                    toast.show();
                monitoreo();
                }
            });
        }
    };
}


public void monitoreo(){
    try{
        StrictMode.ThreadPolicy policy =
                new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

           ResultSet rs = hilo.ConsultaSql("select * from alumno where id_alumno ='" + ids_alumnos[1] + "'");
           if (rs.next()) {
               estado_comparacion = rs.getString("estado");
           }
        comparacion();
    }
    catch (Exception e){
        e.printStackTrace();
        Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
    }
}

public void comparacion() {
                if (estado_actual.equals(estado_comparacion)){
                    Toast.makeText(getApplicationContext(), "No hay cambios", Toast.LENGTH_SHORT).show();
                }else{
                    estado_actual = estado_comparacion;
                    Toast.makeText(getApplicationContext(), "Hay cambios", Toast.LENGTH_SHORT).show();
                }
}
    
asked by Alan Diaz 25.11.2016 в 02:32
source

2 answers

1

It occurs to me to use the class TimerTask to generate a task that is executed every 2 seconds (it is the duration of Toast.LENGTH_SHORT ), in such a way that your code is executed in each repatition, here the code.

    private Timer mTimer1;
    private TimerTask mTt1;

    private void stopTimer(){
        if(mTimer1 != null){
            mTimer1.cancel();
            mTimer1.purge();
        }
    }

    private void startTimer(){
        mTimer1 = new Timer();
        mTt1 = new TimerTask() {
            public void run() {
                monitoreo();
            }
        };

        mTimer1.schedule(mTt1, 1, 2000);
    }

    public void monitoreo(){

        try{

           estado_comparacion= estado_actual;
           ResultSet rs = hilo.ConsultaSql("select * from alumno where id_alumno ='" + ids_alumnos[1] + "'");
           if (rs.next()) {
              estado_comparacion = rs.getString("estado");
           }

           comparacion();

        }
        catch (Exception e){
            e.printStackTrace();
            Toast.makeText(this, e.toString() , Toast.LENGTH_SHORT).show();
        }
    }

    public boolean comparacion() {

        if (estado_actual.equals(estado_comparacion)){
            Toast.makeText(this, "No hay cambios", Toast.LENGTH_SHORT).show(); 
        }else{
            estado_actual = estado_comparacion;
            Toast.makeText(this, "Hay cambios", Toast.LENGTH_SHORT).show();
            a=0;
        }
    }

I hope I serve you.

Greetings!

    
answered by 25.11.2016 / 11:24
source
2

Add the creation of Toast within runOnUiThread so that they can be created without problem:

    public boolean comparacion() {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
           if (estado_actual.equals(estado_comparacion)){
             Toast.makeText(getApplicationContext(), "No hay cambios", Toast.LENGTH_SHORT).show();
             return true;
           }else{
             estado_actual = estado_comparacion;
             Toast.makeText(getApplicationContext(), "Hay cambios", Toast.LENGTH_SHORT).show();
             a=0;
            return  true;
        }

        }
    });   
}
    
answered by 25.11.2016 в 05:51