使用时间轴和 lambda 表达式的定期处理不适用于 Javafx8 for android (gluon)

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

我想使用Timeline 和lambda 表达式进行定期处理。我已经成功地在桌面上测试了实现(见下文)。但是当我通过 Eclipse 和安装 gradle 任务 (Gluon + javafxports) 为 Android 生成它时,它不起作用。 处理一直有效。结果:我认为这就是为什么在测试期间不可能在我的 phone 上检测到触觉触摸屏的原因。

使用java代码:

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): 时间 - 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

多个 periodicProcessing 方法在单个 1758 毫秒内循环调用,而每个方法应每 110 毫秒调用一次

我尝试了其他解决方案,例如避免使用 lambda 表达式,....,结果是一样的:没有生成周期性事件。 但是,无论如何,这些解决方案都适用于桌面。

问题:如何使用 JavaFx 为 Android smartphones 编写定期调用?

Timeline class 的 JavaDoc 中所述,它用于动画,但 "it does not guarantee the timing when KeyFrame is processed"。正是你的问题。

使用 java.util.concurrent.ScheduledExecutorService or at least a java.util.Timer 来完成这类任务。