0

I created a countdowntimer and it starts now if a boolean is true, what would be better if I detect if the timer is running. Is there a way to do that?

I created the following class with the following countdowntimer:

public class GameActivity
{
    int GameTime      = 120;
    long GameTimeLeft = GameTime;
    long GameMillis;

    /** Called when the activity is first created. */
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.gamemap );

        myupdater    = new Handler();
        GameMemory   = new PreferenceHandler(this);
        GameTimeLeft = GameMemory.getMinutesLeft();
        GameMillis   = ( GameTimeLeft * 60000 );

        if ( GameMemory.getTimerSetting() )
        {
            Log.d( "StartTimer", "Minutes Set " + ( GameMillis / 60000 ) );
            GameTimeLeftCounter.start();
            GameMemory.setTimerSetting( false );
        }
    }

    CountDownTimer GameTimeLeftCounter = new CountDownTimer( GameMillis, 60000 )
    {
        public void onTick( long millisUntilFinished )
        {
            GameTimeLeft = millisUntilFinished / 1000 / 60;
            Log.d( "GameTimeLeftCounter", "Time Left: " + GameTimeLeft );
            GameMemory.setMinutesLeft( GameTimeLeft );
        }

        public void onFinish()
        {
            //GAME OVER TIMER FINISHED
            Intent gameover = new Intent( BarcelonaTriviaGame.this, GameOver.class );
            gameover.setFlags( Intent.FLAG_ACTIVITY_NO_ANIMATION );
            Log.d( "TimerFinished", "Finished" );
            startActivity( gameover );
            finish();
        }
    };
Eugene
  • 4,352
  • 8
  • 55
  • 79
Diego
  • 4,011
  • 10
  • 50
  • 76

5 Answers5

2

As you mentioned that you want to use the timer in your game and after some time you want to do game over so best way is the Alarmmanager. When you start the Game set the pending intent for service using the alarm manager.so after the time expire alarm manager will start the service so from that service send the broadcast to the playing activity for Game over.

And also if you want to handle the resume and pause functionality, when user click on the pause button simply cancel the pending intent and save the duration in the preference file. and when user click on the resume button just fetch the time from the preference and calculate the remaining time and set the pending intent of the service using the alarm manager.

You do not worry about the activity to be finished or reCreate.

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • Is there also a possibility then to show the remaining minutes constantly? – Diego Mar 02 '12 at 13:33
  • Yes you can show the remaining minutes by starting the service or thread and update the UI according to it. – Dharmendra Mar 02 '12 at 13:36
  • When you start the game or resume the game start extra service or thread and calculate the remaining minutes and update the UI using broadcast receiver or simply using the handler. – Dharmendra Mar 02 '12 at 13:38
1

Try android:launchMode="singleInstance" in the manifest

JuanMa Cuevas
  • 1,162
  • 9
  • 22
  • I still get multiple timers, I think I need to add some flag to the intent, so it calls the paused activity instead of creating a new one next to the old one. – Diego Mar 02 '12 at 13:07
1

If you want a single timer I think a good idea would be to create a singleton instance of the custom timer object. Every time you want to call the activity that uses the time you just launch the activity and apply the your timer object to the view. The nice thing about this is that you could refer to the same time anywhere in your application (ie. a status bar or a pause menu).

The solution of Bill Pugh @ http://en.wikipedia.org/wiki/Singleton_pattern is a great singleton design pattern to follow.

jjNford
  • 5,170
  • 7
  • 40
  • 64
  • To be save you should implement a private method to start the timer and then call this private method from the private constructor. This will protect the timer from being restarted by other developers who use the class and will guarantee that the timer is started the first time the device is used - that is myTime.getInstance() is called. – jjNford Mar 02 '12 at 14:43
1

Similar to what JuanMa Cuevas said but in the game activity node in the manifest use android:launch mode="singleTask"

This what you need, not singleInstance..

  • I get still the multipletimers, but it doesn´t show the black screen in between. If I manage to check if the timer is running than I can control the start and pause – Diego Mar 02 '12 at 15:10
0

By what you are mentioning that the activity should not appear again, one approach would be calling finish() just before you call the intent. With this, the activity wont be saved in android stack. If you want to end the game, that time call FLAG KEEP NO HISTORY. This will close all the activities which are lined up below top of stack.

Rashmi.B
  • 1,787
  • 2
  • 18
  • 34
  • Hi, the activity should appear again, but now it´s recreated everytime with startActivity(´intent´), which also means that the timer starts over again, so unlimited game time. I use more activities ´behind´ that activity, so I would prefer calling that activity directly instead of using finish(); – Diego Mar 02 '12 at 12:46
  • That means your onCreate is being called again. One approach would be to set a boolean and check. If(true) timer running else if(false) start timer. – Rashmi.B Mar 02 '12 at 13:02
  • Hi Rashmi, but how can I check if the timer is running? – Diego Mar 02 '12 at 13:36