Domanda

I want to save the CountDown Ttimer value into a variable so that next time the user start the Countdowntimer Activity, it to reload at the exact elapsed time as it remained. Here is my code and I don't understand why it doesn't works. (it shows my time Textbox empty)

prefs = PreferenceManager.getDefaultSharedPreferences(this);
   long timer = prefs.getLong("TIME", 86400000);

   new CountDownTimer(timer, 1000) {

         public void onTick(long elapsed) {
             System.out.print(elapsed);
             long timer2=elapsed;
             long hours = timer2 / hours_in_millies;
             timer2 %= hours_in_millies;
             long minutes = timer2 / minutes_in_millies;
             timer2 %= minutes_in_millies;
             long seconds = timer2 / seconds_in_millies;
             time.setText(hours + ":" + minutes + ":" + seconds);
             SharedPreferences.Editor editor = prefs.edit();
             editor.putLong("TIME",elapsed);
             editor.commit(); 


         }
È stato utile?

Soluzione 3

Solved:

  prefs = PreferenceManager.getDefaultSharedPreferences(this);

   xtime = System.currentTimeMillis()-prefs.getLong("TIME",System.currentTimeMillis()); 

   long timer=86400000-(xtime+prefs.getLong("TIME2",0)); 

   final SharedPreferences.Editor editor = prefs.edit();
   editor.putLong("TIME2",xtime+prefs.getLong("TIME2",0));  
   editor.commit(); 


   new CountDownTimer(timer, 1000) { 

         public void onTick(long elapsed) {

             Log.d(TAG, "TIMER" + System.currentTimeMillis());
             long timer2=elapsed;
             long hours = timer2 / hours_in_millies;
             timer2 %= hours_in_millies;
             long minutes = timer2 / minutes_in_millies;
             timer2 %= minutes_in_millies;
             long seconds = timer2 / seconds_in_millies;
             time.setText(hours + ":" + minutes + ":" + seconds);


         }

         public void onFinish() 
         { Intent intent = new Intent(Hug.this, Hug_Accepted.class);
        startActivity(intent);

         }

   }
   .start();    
   xtime=System.currentTimeMillis(); 
    SharedPreferences.Editor editor2 = prefs.edit();
     editor2.putLong("TIME",xtime);         
     editor2.commit(); 

}

Altri suggerimenti

If I am correct, you are updating the shared preferences quite a bit. This is probably a bad idea. It would be better to save the time stamp and the time on the timer on the onStop and then onResume calculate how much time has passed and pick it up from there.

I don't see your timer variable being used in ontick(), you have used timer2 variable. Put a loggerLog.d() before setText() line to check the results for the variables you are using... as otherwise editText cannot be definitely empty

Moreover, save the time in shared preferences in onStop() of your activity.. not on each Tick of timer!

EDIT: After the clarification of requirement from OP

You might want to follow a different approach.. say when the countdown has to start.. use System.currentTimeMillis() to get the START TIME and store it in SharedPreferences/DB.

whenever activity starts... fetch the START_TIME frm preferences... and start timer with initial time as (System.currentTimeMillis - START_TIME) and onTick() update the timer(UI). NOTE: You won't have to update the timer value now back to SharedPreferences

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top