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)