如何在 robovm 中使用 NSTimer?

Ho to use NSTimer in robovm?

我在 robovm 中使用了 NSTimer,但它不起作用,我不知道为什么? 这是我的代码:

mTimer = NSTimer.create(1, new VoidBlock1<NSTimer>() {                  
                    @Override
                    public void invoke(NSTimer a) {
                            onUpdate();
                           System.out.println("onUpdate");
                    }

            }, true);

但没有发生。

其他方式:

mTimer = NSTimer.create(1, this, Selector.register("onUpdate"), null, true);

@Method(selector = "onUpdate")
    public void onUpdate(){
    System.out.println("onUpdate");
}

也没有发生任何事情。其他方式也不行

mTimer = NSTimer.create(1, this, Selector.register("onUpdate:"), null, true);

@Callback @BindSelector("onUpdate:")
public void onUpdate(Selector cmd){
    System.out.println("onUpdate");
}

它也不起作用。请帮助我。

我还没有对此进行测试,但查看 NSTimer.java 似乎您正在创建一个没有 starting/firing 的计时器。

也许你可以尝试方法 createScheduled():

mTimer = NSTimer.createScheduled(1, new VoidBlock1<NSTimer>() {                  
                @Override
                public void invoke(NSTimer a) {
                        onUpdate();
                       System.out.println("onUpdate");
                }

        }, true);

或者,如果您真的不需要它作为 NSTimer,我建议您使用 java.util.Timer。这在 Robovm 应用程序上非常有效。