-1

Im making a game on android and i want it to run for like 60 seconds and the display game over. much like fruit ninja. how do i handle the time in this case? is the android.timer of much use or the alarm manager?

4

2 回答 2

2

You can use a CountDownTimer:

private void initTimer() {      
    mWaitTime = 60 * 1000;
    mWaitTimer = new CountDownTimer(mWaitTime, mWaitTime) {

        public void onTick(long millisUntilFinished) {/* Do nothing */ }

        public void onFinish() {
            // stop your game...
        }
    }.start();
}

Be sure to cancel the timer on the onPause method:

mWaitTimer.cancel();
于 2013-01-15T15:30:40.093 回答
0

Use an AsyncTask, run a Thread with the sleep method (in the doInBackground method) for 1 second do this 60 times and after each second you can update the progress so the method onProgressUpdate will be called each second, after the thread has runned 60 times the AsyncTask will go into the onPostExecute where you can finish your application or do whatever you want to do when the time has run out.

于 2013-01-15T16:30:56.010 回答