Home android Timer in the Android application

Timer in the Android application

Author

Date

Category

How to make a timer in an Android application? Just use java.util.timer ?


Answer 1, Authority 100%

There is in the OS that – countdowntimer .
But in general, here is a suitable:

Timer Timer = New Timer ();
Timer.Schedule (New updatetimetask (), 0, 1000); // Tick every second without delay
// Task for timer
Class UpdateTimetask EXTENDS Timertask {
  Public void Run () {
    ...
  }
}

In fact, try first Countdowntimer .
A, remembered. Here you still have dock .

Only the timer works once (the one java.util.timer ), such a feature is, and you need to intercept illegalstateException .


Answer 2, Authority 73%

You can use CountdownTimer.

Public Class Mytimer Extends Countdowntimer
{
  Public Mytimer (Long MillisInfuture, Long CountdownInterval)
  {
     Super (MillisInfuture, CountdownInterval);
  }
  @Override
  Public void onfinish ()
  {
    // Do Something ...
  }
  Public Void Ontick (Long Millisuntilfinished)
  {
  }
}

Answer 3, Authority 9%

If you need a simple timer, you can use chronometer .

For example, in Layout We place Chronometer :

& lt; chronometer
Android: id = "@ + id / chronometer"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" / & gt;

in Activity Initialize Chronometer , get the time since the application starts and install it as the basic CRONOMETER :

Chronometer Chronometer = (Chronometer) FindViewByid (R.ID.Chronometer);
Long StartTime = SystemClock.elapsedrealtime ();
Chronometer.SetBase (StartTime);

We get a regular tick timer on the screen.

Time output format can be changed in SetFormat (String Format)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions