简体   繁体   中英

CountdownTimer isn't working

I need a CountDownTimer that saves it's remaining time after the application closes as well. I have this code, and I'm not really sure if my HH:DD format is bad or the SharedPreferences aren't working well. Also, If someone may modify this code to work with seconds too, I will be very thankful.

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);


   new CountDownTimer(86400000, 1000) {

         public void onTick(long elapsed) {

             if(elapsed/3600>=10&&(elapsed/60000)/24>=10)           
                    time.setText(elapsed/3600000+":"+(elapsed/60000)/24);
                    else 
                        if(elapsed/60000>=10&&(elapsed/60000)/24<10)
                        time.setText(elapsed/36000+":"+"0"+elapsed/60000);               
                        else if(elapsed/60000<10&&(elapsed/60000)/24>10)
                            time.setText("0"+elapsed/36000+":"+elapsed/60000);      
                            if(elapsed/60000<10&&(elapsed/60000)/24<10)
                            time.setText("0"+elapsed/36000+":"+"0"+elapsed/60000);  

                              elapsed = prefs.getLong("TIME", 86400000);
                              SharedPreferences.Editor editor = prefs.edit();
                              editor.putLong("TIME",elapsed);
                              editor.commit(); 
         }
         public void onFinish() {

         }
      }.start();

}

I'm never tried it but in ANDengine library for 2d games there is an example of a chronometer, so you maybe you can look at it!! it comes with seconds and milliseconds. here you will find more information about the library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM