Refresh clock

0
import android.annotation.TargetApi;
import android.icu.util.Calendar;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import java.util.Date;


public class MainActivity extends AppCompatActivity {
    @TargetApi(Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    Thread myThread = null;
    Runnable runnable = new CountDownRunner();
    myThread= new Thread(runnable);
    myThread.start();
}


public void doWork() {
    runOnUiThread(new Runnable() {
        @TargetApi(Build.VERSION_CODES.N)
        public void run() {
            try{
                Button  botonJAVA = (Button)findViewById(R.id.botonXML);
                Button  boton2JAVA = (Button)findViewById(R.id.boton2XML);
                TextView txtCurrentTime= (TextView)findViewById(R.id.hola);
                TextView hora= (TextView)findViewById(R.id.hola2);



                Calendar c = Calendar .getInstance();
                int second = c.get(Calendar.SECOND);
                int minute = c.get(Calendar.MINUTE);
                int hour = c.get(Calendar.HOUR);
                String time = hour+":"+minute+":"+second;
                txtCurrentTime.setText("La hora es : " + time);


                Date dt = new Date();
                int hours = dt.getHours();
                int minutes = dt.getMinutes();
                int seconds = dt.getSeconds();
                String curTime = hours + ":" + minutes + ":" + seconds;
                hora.setText(curTime);
                if ((hours==10)&&(minutes==00))
                {
                    botonJAVA.setEnabled(true);
                }
                else
                {
                    botonJAVA.setEnabled(false);
                }
                if ((hour==12)&&(minutes==00))
                {
                    botonJAVA.setEnabled(false);
                }


                if ((hour==4)&&(minutes==00))
                {
                    boton2JAVA.setEnabled(true);
                }
                else
                {
                    boton2JAVA.setEnabled(false);
                }
                if ((hour==7)&&(minutes==00))
                {
                    boton2JAVA.setEnabled(false);
                }
            }catch (Exception e) {}
        }
    });
}

class CountDownRunner implements Runnable{
    // @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()){
            try {
                doWork();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }catch(Exception e){
            }
        }
    }
}

}

    
asked by GOMEZ MUÑOZ BRAYAN 24.10.2016 в 17:39
source

1 answer

2

You need a thread, which is checking the condition every so often

class MiHilo extends Thread {
    public void run() {
        int bandera = true;
        do{
            Calendar c = Calendar.getInstance();
            int seconds = c.get(Calendar.SECOND);
            int minutes = c.get(Calendar.MINUTE);
            int hour = c.get(Calendar.HOUR);
            String time = hour+":"+minutes+":"+seconds;
            txtHour.setText("Tiempo es de : " + time);
            if ((hour==3)&&(minutes==23))
            {
               botonJAVA.setEnabled(true);
               bandera = false;
            }
            Thread.sleep(1000) //para que se revise cada segundo, va por milisegundos
        }    
        while (bandera == true);
    }
}

Then in the main() or where you occupy it, you call the thread

Thread hiloReloj = new Thread( new MiHilo(),"hiloReloj " );
hiloReloj.run();
    
answered by 24.10.2016 / 17:56
source