In the Android SDK, a timer is provided for us. This timer is called Chronometer. We can use it as a component of Android, and it also has its own unique method. The following is an example of how to use the timer and common methods.
Like other UI components, when we want to use it, the corresponding position in the layout file declares the location and attribute of the timer.
Copy codeThe Code is as follows: <Chronometer
Android: id = "@ + id/chronometer"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
Use the defined timer in the program and set the display time format.Copy codeThe Code is as follows: chronometer = (Chronometer) findViewById (R. id. chronometer );
Chronometer. setFormat ("Timing time :( % s )");
In this way, the program will display the timer content set above. In addition, we can use the following common timer methods in the program to control the timer work.Copy codeThe Code is as follows: A, Chronometer. start (); // start time
B. Chronometer. stop (); // pause the timer.
C. Chronometer. setBase (SystemClock. elapsedRealtime (); // reset the timer to stop the timer.
Android is such a control that allows us to easily implement timing work in the program, thus eliminating complicated threads and redundant code writing, saving a lot of development time.
Usage of Chronometer for Android
Step 1: layout the file:
The main. xml Code is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Chronometer
Android: id = "@ + id/myChronometer"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<Button
Android: id = "@ + id/btn_start"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = ""/>
<Button
Android: id = "@ + id/btn_stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "stop"/>
<Button
Android: id = "@ + id/btn_base"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "reset"/>
<Button
Android: id = "@ + id/btn_format"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "formatting"/>
</LinearLayout>
</LinearLayout>
Step 2: MainActivity
The Code is as follows:Copy codeThe Code is as follows: package net. loonggg. chronometer;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. SystemClock;
Import android. OS. Vibrator;
Import android. view. View;
Import android. widget. Button;
Import android. widget. Chronometer;
Import android. widget. Chronometer. OnChronometerTickListener;
Public class MainActivity extends Activity {
Private Vibrator vibrator;
Private Chronometer chronometer; // timing component
Private Button btn_start;
Private Button btn_stop;
Private Button btn_base;
Private Button btn_format;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE); // gets the vibration Service
Chronometer = (Chronometer) findViewById (R. id. myChronometer );
Chronometer
. SetOnChronometerTickListener (new OnChronometerTickListenerImpl (); // set the ship object for the timing component
Btn_start = (Button) findViewById (R. id. btn_start );
Btn_stop = (Button) findViewById (R. id. btn_stop );
Btn_base = (Button) findViewById (R. id. btn_base );
Btn_format = (Button) findViewById (R. id. btn_format );
Btn_start.setOnClickListener (new ButtonClickListener ());
Btn_stop.setOnClickListener (new ButtonClickListener ());
Btn_base.setOnClickListener (new ButtonClickListener ());
Btn_format.setOnClickListener (new ButtonClickListener ());
}
Public class OnChronometerTickListenerImpl implements // time listening event, monitoring time changes anytime, anywhere
OnChronometerTickListener {
@ Override
Public void onChronometerTick (Chronometer chronometer ){
String time = chronometer. getText (). toString ();
If ("". equals (time) {// shake the phone five seconds later
Vibrator. vibrate (new long [] {1000, 10,100, 10}, 0); // you can set the vibration cycle and whether to perform cyclic vibration. If you do not want to perform cyclic vibration, change 0 to-1.
}
}
}
Public class ButtonClickListener implements View. OnClickListener {
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btn_start:
Chronometer. start (); // start timing
Break;
Case R. id. btn_stop:
Chronometer. stop (); // stop timing
Break;
Case R. id. btn_base:
Chronometer. setBase (SystemClock. elapsedRealtime (); // reset key
Break;
Case R. id. btn_format:
Chronometer. setFormat ("display time: % s."); // you can change the display time format.
Break;
Default:
Break;
}
}
}
}
Step 3: Register Permissions:Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. VIBRATE"/>
You do not understand the world of the arms, you do not know the life of the arms, and you only know the world of programmers. Programmers, strive for their own wonderful world, and work hard! Come on ......