Timer stops working after app resume (Android)

2019-08-08 20:36发布

I'm using three timer to trigger different periodic tasks (not all enabled at the same time). However after pausing/resuming the app (by switching to a different app), some of the timers stop working. After reading through the web I found that there was a bug in libGDX related to this, but it has already been fixed. Awkwardly enough, only two of the timers stop working while the third one works fine.

@Override
public void create ( ) {
    createTimers();
    easterTimer.start();
    inputTimer.stop();
    refreshTimer.start();

    //More stuff...
}


//Called by "onRestart()", on other class, after calling super.onRestart method
public void restart(){
    createTimers();
    simulationStatus = simulationStatus_stop;
    selectedOption = -1;
    draggingFinger = false;
    refreshTimer.stop();
    easterTimer.start();
    inputTimer.start();



}


private void createTimers() {
    easterTimer = new Timer();
    easterTimer.scheduleTask(new Timer.Task() {
        @Override
        public void run() {
            System.out.println("easter");
            //Do stuff....
        }
    }, 0, 0.25f);


    inputTimer = new Timer();
    inputTimer.scheduleTask(new Timer.Task() {
        @Override
        public void run() {
            System.out.println("input");
            //Do stuff....
        }
    }, 0, 0.01f);


    refreshTimer = new Timer();
    refreshTimer.scheduleTask(new Timer.Task() {
        @Override
        public void run() {
            System.out.println("refresh");
            //Do stuff....
        }
    }, 0, (float) 1 / 80);
}

Please note that in the following situation the "refreshTimer" is disabled, so it should not be printing anything -- as it's doing. Though I have tested and this Timer is the only one that works properly after resume.

08-07 23:52:52.450    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:52.450    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:52.450    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:52.490    8762-8762/com.asjv.echarges.android I/AndroidInput﹕ sensor listener tear down
08-07 23:52:52.490    8762-8805/com.asjv.echarges.android I/AndroidGraphics﹕ paused
08-07 23:52:58.430    8762-8762/com.asjv.echarges.android I/AndroidInput﹕ sensor listener setup
08-07 23:52:58.490    8762-8805/com.asjv.echarges.android I/AndroidGraphics﹕ resumed
08-07 23:52:58.490    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ easter
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ easter
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:58.500    8762-8805/com.asjv.echarges.android I/System.out﹕ input
08-07 23:52:59.140    8762-8762/com.asjv.echarges.android I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41cbafa0 time:92087596

The bottom line is: does anyone more knowledgeable than me have any hint on why this may be happening? Or even what may be triggering the log "Activity_idle id:", since its after that that the timers seem to stop working?

For reference, I'm developing for Android and using libGDX 1.6.4

0条回答
登录 后发表回答