Rather than maintaining a thread, I recommend just using a
CountDownTimer: 
http://developer.android.com/reference/android/os/CountDownTimer.html

Of course that still leaves your pause problem. One way to do it is:
1) when the user starts, kick off the CountDownTimer and store the
start time, say in a variable startTime.
2) if the user pauses, then cancel the CountDownTimer, and store the
time elapsed so far (current time - startTime) in another variable,
say timeElapsedSoFar.
3) when the user resumes, set a new CountDownTimer, which is shorter
than the original by timeElapsedSoFar.

I skipped a few details for the sake of brevity, but I hope you get
the idea.


Yusuf Saib
Android
·T· · ·Mobile· stick together
The views, opinions and statements in this email are those of the
author solely in their individual capacity, and do not necessarily
represent those of T-Mobile USA, Inc.



On Jul 20, 11:56 am, "[email protected]" <[email protected]>
wrote:
> hello there
> I'm a newbie in android.
> I'm tryin to develop a game checking user's knowledge. for every
> question asked , the player has 20 secs to answer. otherwise he loses.
> I'm trying to pause my activity when clicking menu, however i don't
> know how to pause the timer thread.
> In my menu i have 2 options( new game or quit).  so i need to pause
> everything when clicking the menu and resuming when clicking it again.
>
> PLEASE HELP.
> Thank you
>
> that's how i'm keeping track of time:
>
>     public void timedOut()
>                {                        //called when time limit is reached 
> for input
>                     timer.interrupt();                                  
> //stop the timer thread
>                     lost = true;                                        
> //player lost, so set lost variable
> to true
>                     input_text.setText("");                   //clear 
> input_text area of
> any text
>                  Toast.makeText(getBaseContext(), "You took TOO LONG!",
> Toast.LENGTH_SHORT).show();
>                  Toast.LENGTH_SHORT).show();
>                  disptext.setVisibility(4);             //remove text display 
> area
>                  endgame();
>     }
>
>     public class timerThread extends Thread {                   //times user 
> input
>         public int time = 0;                                                  
>   //keeps track of time in hundreds of
> milliseconds
>         public int timeLimit =200;                                            
>   //initial time limit (20 seconds)
>
>         public void run() {
>                 time = 0;
>                 while(time < timeLimit) {                    //as long as 
> time limit has not been
> reached,
>                         if (isInterrupted()) {return;}             //if 
> interrupted
> before sleep, we will know
>                         time++;                                               
>   //add another 200 ms to time
>                         try {
>                                         sleep(100);                           
>           //and sleep for 200 ms
>                                 } catch (InterruptedException e) {
>                                         return;                               
>           //leave run() if interrupted
>                                 }
>                 }
>
>                 handleTimer.post(timeUp);                       //when player 
> input is too late,
> pass timeUp to the handler
>         }
>
> that's how i'm using the menu:
>
>     public boolean onCreateOptionsMenu(Menu menu) {
>
>          super.onCreateOptionsMenu(menu);
>              getMenuInflater().inflate(R.menu.menu, menu);
>              playgame.getThread().onPause();
>
>              return true;
>         }
>
> public boolean onOptionsItemSelected(MenuItem item) {
>         super.onPause();
>         playgame.getThread().onPause();
>   switch (item.getItemId())
>     {
>         case R.id.quit:
>                 finish();
>                 return true;
>         case R.id.new_game:
>                 newGame();
>                 return true;
>
>     }
>              return false;
>
> }
>
> @Override
> protected void onPause() {
>     super.onPause();
>     SimonSays.getThread().onPause();
>
>
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to