Use of android timer of Chronometer and common methods

  • 2020-05-09 19:14:00
  • OfStack

In SDK of Android, we are provided with a timer, which is called Chronometer. We can call it a component of Android, and it also has its own unique method. Let's give an example of how to use this timer and how to use it.

Like the other UI component 1, when we use it, we define the location and properties of the timer in the corresponding location declaration in the layout file.

 
<Chronometer 
android:id= " @+id/chronometer "  
android:layout_width= " wrap_content "  
android:layout_height= " wrap_content "  
/> 

Use a timer defined in the program and set the display time format.
 
chronometer = (Chronometer) findViewById(R.id.chronometer); 
chronometer.setFormat( "Timing time: (%s) " ); 

In this way, the timer content set above will be displayed after the program is run. In addition, we can control the timer by the following common timer methods in the program.
 
A , Chronometer.start();// Timing begins  
B , Chronometer.stop();// Suspension of timing  
C , Chronometer.setBase(SystemClock.elapsedRealtime());// Reset timer, stop timing  

Android is through such a control, so that we can easily achieve the work of timing in the program, so as to avoid complex threads and redundant code writing, save a lot of development time.

Use of the Android timer (Chronometer)
Step 1: layout file:
main.xml 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=" start " /> 
<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:
 
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; //  The 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);//  Get 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 //  Timing listen for events, and listen for changes in time anytime, anywhere  
OnChronometerTickListener { 
@Override 
public void onChronometerTick(Chronometer chronometer) { 
String time = chronometer.getText().toString(); 
if ("00:05".equals(time)) {//  judge 5 Seconds later, let the phone vibrate  
vibrator.vibrate(new long[] { 1000, 10, 100, 10 }, 0);//  Set the vibration period and whether to cycle the vibration, if you do not want to cycle the vibration 0 Instead of -1 
} 
} 
} 
public class ButtonClickListener implements View.OnClickListener { 
@Override 
public void onClick(View v) { 
switch (v.getId()) { 
case R.id.btn_start: 
chronometer.start();//  Start the time  
break; 
case R.id.btn_stop: 
chronometer.stop();//  Stop timing  
break; 
case R.id.btn_base: 
chronometer.setBase(SystemClock.elapsedRealtime());//  Reset button  
break; 
case R.id.btn_format: 
chronometer.setFormat(" Display time: %s.");//  Change the time display format  
break; 
default: 
break; 
} 
} 
} 
} 

Step 3: registration rights:
 
<uses-permission android:name="android.permission.VIBRATE" /> 

You don't understand the world of bugs. You don't understand the world of programming apes. Program apes, for their own wonderful world struggle, work hard! Come on...


Related articles: