What command do I use to update my code every 2 seconds?

0

I'm developing an app in Android Studio that takes the location of the device and returns me in latitude and longitude, I use it later to make a bookmark. However, if I install the application on my Smartphone in one neighborhood and go to another the Application does not open any more, it is giving execution error. I believe it only runs the code only once, so I would like to know a command for the Application to always update the code every 2 seconds or anyone who knows another solution to my problem would appreciate the help.

    
asked by anonymous 18.10.2017 / 14:23

1 answer

-2

You can implement or approach something using threads to update your object every X seconds:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

for(;;)
{
  new Handler().postDelayed(new Runnable(){
    @Override
    public void run() {
        //Faz as atualizações que você precisa.
    }
}, 2000);   // Tempo em milisegundos 2000 = 2 segundos
}

}

I suggest that you put a limit on the counter or a condition on for(;;) for your tests ...

    
18.10.2017 / 14:36