My listener ChangeListener is triggered a second time in a second, and I need only the most recent of its operation from this heap (so as not to produce a lot of requests). If I wrote a JavaScript program, then I would process each trigger with the setTimeout function with 1 second interval, if this trigger should be followed by the next one, then I would cancel the first setTimeout and set a new one, as a result only a single trigger would be executed in a second. How do I implement this in Java?

Here is filtrRequestAdres(center); runs a bunch of times. How to implement onCameraChange filtering in it?

 mMap.setOnCameraChangeListener(new OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition pozCamera) { filtrRequestAdres(center); } }); public void filtrRequestAdres(LatLng center) { // if(...) { startIntentService(); } } 

How to postpone execution of a function in Java and how to cancel a deferred execution of a function? And so that the work of the delay program is not affected. :)

This is the thing that almost solves my question:

 new android.os.Handler().postDelayed( new Runnable() { public void run() { Log.i("tag", "This'll run 300 milliseconds later"); } }, 300); 

It defers execution, but how to cancel this deferred execution?

  • what you need is a handler. In particular, the [postDelay] method ( developer.android.com/reference/android/os/… , long)). - KoVadim
  • Thank you, what is necessary, and how to cancel the execution of the deferred? - Gennady
  • And you try to look in the link below a little bit, in the following method, which is called removeCallbacks. - KoVadim
  • @Gennadiy Kozlov, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

2 answers 2

You can do without a handler. Everything is much easier.

 mMap.setOnCameraChangeListener(new OnCameraChangeListener() { long previousSystemTime = System.currentTimeMillis(); //Объявляем поле, которое будет хранить предыдущее время срабатывания @Override public void onCameraChange(CameraPosition pozCamera) { if((System.currentTimeMillis() - previousSystemTime) > 1000) { // если текущее время минус предыдущее больше 1000 миллисекунд filtrRequestAdres(center); // то делаем, что надо previousSystemTime = System.currentTimeMillis(); // и записываем новое значение предыдущего времени } } }); 
  • And what is the best way to use this or with postDelay? What will be more productive? - Gennady
  • And if at this second the system time is lost? - Flippy
  • @Flippy, System.currentTimeMillis () is not related to what you call system time. - Vladyslav Matviienko
  • It seems to have it directly. - Flippy 1:56 pm
  • @Flippy, how can it get off? - Vladyslav Matviienko 1:59 pm

There is a setTimeout () method in the underscore-java library.

Example with code:

 final Integer[] counter = new Integer[] {0}; Supplier<Void> incr = new Supplier<Void>() { public Void get() { counter[0]++; return null; } }; U.setTimeout(incr, 100); 

Execution will begin in 100 ms in the new thread.