I have been suffering for 5 hours already, I can not guess how to do it:

In general, there is a code requesting the values ​​(coordinates) of a marker. So, at the very beginning - the variables for it are zero, after the answer came from the server with the coordinates - it needs to be placed on the map. It is necessary to make a delay of about 3-4 seconds while not slowing down the rest of the application code. How can I do that?

  • one
    Read about multithreading. Create a separate Thread and execute the code in it after the delay through sleep . - Maksim Bogdanov
  • If you are given an exhaustive answer, mark it as accepted ^ _ ^ " - Suvitruf

1 answer 1

I do not recommend creating threads for such things.

If the gap is small, then it is better to use ScheduledExecutorService .

For long pauses it is better to use BroadcastReciever and AlarmManager .

Example with ScheduledExecutorService :

 public class MyActivity extends Activity { private ScheduledExecutorService mScheduleTaskExecutor; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mScheduleTaskExecutor = Executors.newScheduledThreadPool(1); // будет вызвано через 3 секунды mScheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public void run() { // если что-то в гуи делать надо runOnUiThread(new Runnable() { public void run() { // делаем что-то в гуи } }); } }, 3, TimeUnit.SECONDS); } }