0

I have an activity in which i need to implement a basic mm::ss timer . Below is the code i have written for it. However the problem is that when i press the back button in the emulator and click on the app again, the values change much faster. It looks like the onCreate is being called again. How do i rectify this? I have tried creating a boolean variable and setting it to true the first time the task is invoked . I call the startPeriodidUpdates() only when the value is false.But the onCreate creates the variable again with the value of false.

   public class GraphicsActivity extends Activity {
static int seconds = 0;
static int minutes = 0;
public static String time_elapsed;
public static boolean clearView = true;
Handler myhandler = new Handler();
public static String min,sec;  
public static boolean running = true;


   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     startPeriodicUpdates();

    } 
    public void onResume(){
    super.onResume();
    } 
    public void onPause() {
    super.onPause();    
    }
   public void onStop(){
   super.onStop();
    }

   public void startPeriodicUpdates()
   {
periodicCall();
   }
   public void stopPeriodicUpdates(){ 
     myhandler.removeCallbacksAndMessages(myhandler);
   }

   public  void periodicCall()
    {

seconds++;
if(seconds ==60)
{
    seconds=0;
    minutes++;
    if(minutes==60)
    {
        seconds=0;
        minutes=0;
    }
}
// left-padding zeros to the minutes and seconds values
min = String.format("%02d",minutes);
sec = String.format("%02d",seconds);
time_elapsed =  min + ":" + sec;
time_elapsed =  min + ":" + sec + "";
myhandler.postDelayed(new Runnable(){
    public void run(){
        periodicCall();

    }
},1000);

}

hektor
  • 1,017
  • 3
  • 14
  • 28

4 Answers4

1

Perhaps this will help: The right way to do a peridic task is registering the handler in the OnResume and unregister in in OnPause. (You can unregister it other places, but OnPause is important)

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • Yes thats the right thing to do, but in my case i want the timer to be running in the back-ground even if i press the back-button in the emulator. – hektor Apr 24 '12 at 05:30
1

I think its better if you use a Service for implementing your timer. Even if you push back button, the Service will continue running.

Here you can see a question I asked a bit time ago. You can see the implementation of a Custom Chronometer using a Service.

The activity where you want to receive the value of the Chronometer just need to have a BroadcastReceiver like:

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        mMilis = intent.getLongExtra("milis",0);
        String time = intent.getStringExtra("tiempo");
        // Do Something with the time
    }
};
Community
  • 1
  • 1
gutiory
  • 1,135
  • 5
  • 13
  • 33
0

This may not be a solution to the problem above, but if you just want to update a view and count up form a given time, maybe have a look at Chronometer. If you want to count down and update views, you can do that with CountDownTimer

jlindenbaum
  • 1,891
  • 18
  • 28
0

You can use shared preference to use this method only once .

//pass true first time

 protected void storeSharedPrefs(Boolean value) {
        /*
         * Storing in Shared Preferences
         */
        editor.putString("first", value);
        editor.commit();  //Commiting changes
    } 

Check each on time application is loaded, whether its the first time and configuration details has been entered correctly by checking SharedPreferences

private boolean first_time_check() {
        /* 
         * Checking Shared Preferences if the user had pressed 
         * */
        Boolean  first = uPreferences.getString("first", false);

            return first;
    }
dharmendra
  • 7,835
  • 5
  • 38
  • 71