SHARE
TWEET

AsyncTimerIssues

a guest Jan 26th, 2020 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         @Override
  2.         protected String doInBackground(Integer... params) {
  3.  
  4.             long startMillis = System.currentTimeMillis();
  5.             long timerLengthInMillis = params[0];
  6.             long endTimeInMillis = startMillis + timerLengthInMillis;
  7.             long lastTimerCheck = System.currentTimeMillis();
  8.             long secondTimerCheck;
  9.             long hasItBeenASecond = 0;
  10.  
  11.             while(System.currentTimeMillis() < endTimeInMillis){
  12.                 try {
  13.  
  14.                     secondTimerCheck = System.currentTimeMillis();
  15.                     hasItBeenASecond = secondTimerCheck - lastTimerCheck;
  16.  
  17.                     if (hasItBeenASecond < 1000) {
  18.  
  19.                         Thread.sleep(250);
  20.  
  21.                         System.out.println(hasItBeenASecond + " has it?");
  22.  
  23.                     } else {
  24.  
  25.  
  26.                         lastTimerCheck = System.currentTimeMillis();
  27.                         hasItBeenASecond = 0;
  28.  
  29.                         long remainingTime = endTimeInMillis - System.currentTimeMillis();
  30.                         publishProgress((int) remainingTime);
  31.  
  32.                     }
  33.                 }catch(InterruptedException e){
  34.                     System.out.println("Sleep interrupted");
  35.                 }
  36.             }
  37.  
  38.             return "";
  39.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top