Hey there.
So I created a new project in Android Studio and name it.
Cool timing in it.
We will get acquainted with several new development concepts we will learn how to use time in the application.
Also we will learn how to create menus.
And we will learn how to create the user settings in the application.
First I want to show you two ways to create timers in Android.
The first is using a handler.
Let's create an object of class handler in the create method we write.
Handler handler and write as son you handle next.
We must create an object of the original class so that handler can run this object.
Right or animal reasonable assigned new random and press enter and Android Studio creates for us a new
anonymous class inherited from Iran symbol.
I hope you remember this form of writing.
We started it and in this anonymous class the run method is over a reader.
And in this round method we must specify the code that will be executed.
Let's just output some kind of record to look in this situation.
We'll write a log.
The in the debug mode in specified roundtable here and for example let's print some text every two seconds
for example two seconds are passed and then.
So for this code to be executed every two seconds we must specify the forward we must specify handler
object.
Here next call the post delayed method of these class specify random ball using keyword these and the
second argument.
We must specify the delay itself was delayed.
This means sending with a delay.
We must indicate the delay here and since we want this happen every two seconds we specify the delay
in milliseconds of two thousand milliseconds.
Here we must put a cynical and further we below.
We must start this process and this is done using the handle.
We simply write post and specify our animal.
Now we can launch the app and check call.
This will all happen.
So our application is running.
Let's open we'll cut select the debug mode.
And copy the SKU and as you can see this takes two seconds.
Our past is displayed every two seconds.
That is the program works correctly.
The method that are sure to you first is suitable not only for timers but also for different situations.
When you want to start some process later at some point that is a riot some code in or animal and then
in another place or the program you can call this code using the handle or POS method.
Well for the timer there is another way in my opinion more convenient let me comment on the previous
code
and write the next one for the second method.
We use the class countdown timer
let's call it my timer and then assign new countdown timer and again and the studio creates an anonymous
class force with over methods.
Put a semicolon here in these methods.
We must write our code and also you see here the parameters are underlined.
We need to pass two parameters of type or that first parameter indicates how long this code will be
executed.
That is the length of the time when the timer will work.
For example we can specify two seconds in milliseconds.
It will be ten next.
Put a comma as specified the second parameter.
This is the interval with what frequency the timer will work.
If one second is or 1000 below seconds further in their own tick method.
In this method the court will be executed every second.
This is the interval that is specified in our parameter.
Let us again write something in a log.
Here specify the key my timer.
And here we have to write a message.
Let's get how many seconds are left to do this.
Use these parameter mails until finished milliseconds until it is finished.
Since we need to use that type three.
All right string value of
and then specify a bedroom at meals until finished
and divided by a thousand so that the result is in there in seconds.
And also add text let me bring it to another line so that it can be better see plus and specify the
text seconds left.
Put a semicolon and in the own finished method.
This method is executed when the time it ends it's countdown.
Again we specify here output in the log with the same key
and here we simply display the finish
and then to start this code we need to start our time.
Right my timer and start
let's run and see what habit
is.
We need to specify the key card here and as you can see everything worked.
But let's run it again so that it can be seen.
Least going down is feasible
so nine eight seven six five 4 3 2 1 0 and finish as that is everything worked for us.
Everything turned out.
And in the next two we do we will begin to create our cool timer application.