Android timer that works when device is sleeping

后端 未结 2 748
灰色年华
灰色年华 2020-12-30 06:20

I\'m writing a sports app that needs to track the elapsed time of quarter/half/period. Elapsed time needs to be accurate to the second. The game clock needs to continue to

2条回答
  •  长发绾君心
    2020-12-30 06:48

    I'm sorry this is not quite an answer, but I feel your process is similar to mine, but there was little in regards to code to make it clear how you got around the sleep issue. I don't have drift, but the app does hang when it goes into sleep mode,then kind of resets forward when the display is active, then hangs again when the device sleeps. This is the core of the timer process.

        Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {
    
        @Override
        public void run() {
    
            // do something here to display
    
            processTime();    // process what to be done on a sec by sec basis
            try {
                timerHandler.postDelayed(this, 1000
            } catch (Exception ex){
    
            }
    
        }
    };
    

    Is there something here that I can do to allow it to continue when in sleep mode? This use to work on older version of android/devices.

提交回复
热议问题