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

Asked

Viewed 235 times

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 marker. However, if I install the application on my Smartphone in one neighborhood and go to another the App does not open anymore, is giving error in execution. I believe it only runs the code once, so I would like to know a command for the App always update the code in 2 in 2 seconds or who knows another solution to my problem would appreciate the help.

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 you put a limit on the counter or a condition on the for(;;) for your tests...

  • 1

    In addition to being a bad solution, the use of for(;;) will cause it to repeat itself indefinitely.

  • I pointed out a solution, I never said it was the best... And as I said in the reply, the user can put a counter or a condition to stop the for(;;)

  • 1

    We must find solutions that work without bringing bigger problems than those that aim to solve. :)

  • Well, your term "solutions that work" is a little vague don’t you think ?... My solution works for his problem... It can make adaptations or customizations depending on the need. I just tried to help in a simple and functional way..

  • 1

    I don’t agree with "My solution works...", but I’m not going to argue with you, it’s a "matter of opinion".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.