Search code examples
androidtimelinegluonjavafxportsperiodic-task

Periodic processing with Timeline and lambda expression does not work with Javafx8 for android (gluon)


I want to make a periodic processing by using Timeline ad lambda expression. I have tested the implementation (see below) on desktop, with success. But it does not work when I generate it for Android, through Eclipse and Install gradle task (Gluon + javafxports). The processing works all the time. Consequence: I think that is why there is no possibility to detect a tactile touchScreen on my phone during a test.

With the java code:

static long BDTapplication = 110; // in ms

static long nbBDT = 0;

static long t0 = System.currentTimeMillis();


/********************************************************/

public static void periodicProcessing() {
    nbBDT++;
    traceWithSystemTime("periodicProcessing / nbBDT: " + nbBDT);
}

/********************************************************/
public static void traceWithSystemTime(String comments) {
        long t =  System.currentTimeMillis();
        long deltaT =t - t0;
       System.out.println("time - t0: " + t0 + " /  t: "+ t + " / detaT: " + deltaT + " ms --- " + comments);
}

/********************************************************/

public void start(Stage primaryStage) {

    primaryStage.setTitle("Hello World sur Android! -------------->>>>>>>>>>>>>>>>>>>");

    // Timeline and BDT - section with pb
    Timeline traittBDTapplication = new Timeline(
              new KeyFrame(Duration.millis(BDTapplication), ae ->periodicProcessing()));
        traittBDTapplication.setCycleCount(Timeline.INDEFINITE);
        traittBDTapplication.play();
// End of section pb

I/System.out(23475): time - t0: 1484454901468 / t: 1484454903224 / detaT: 1756 ms --- Fin du test

I/System.out(23475): time - t0: 1484454901468 / t: 1484454903226 / detaT: 1758 ms --- periodicProcessing / nbBDT: 1

I/System.out(23475): time - t0: 1484454901468 / t: 1484454903226 / detaT: 1758 ms --- periodicProcessing / nbBDT: 2

I/System.out(23475): time - t0: 1484454901468 / t: 1484454903226 / detaT: 1758 ms --- periodicProcessing / nbBDT: 3

Several periodicProcessing methods are called in loop in a single 1758 ms whereas each of them should be called at every 110 ms

I have tried other solutions like for avoiding to use the lambda expression, ...., results are the same: no periodic event is generated. But, in any case, these solutions work on desktop.

Question: how to write a periodic call by using JavaFx for Android smartphones?


Solution

  • As mentioned in the JavaDoc of the Timeline class, it is intended for animation, but does "it does not guarantee the timing when KeyFrame is processed". Excatly your issue.

    Use either a java.util.concurrent.ScheduledExecutorService or at least a java.util.Timer for these kind of tasks.