The use of the Android timer (chronometer) and the usual methods _android

Source: Internet
Author: User

In the Android SDK, we have a timer, called chronometer, that we can become a component of Android, and it has its own unique approach. Here are some examples of how this timer is used and how it is used.

As with other UI components, when we want to use it, we declare the position and attributes of the timer in the corresponding position in the layout file.

Copy Code code as follows:

<chronometer
Android:id= "@+id/chronometer"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>

Use the defined timer in your program and set the display time format.
Copy Code code as follows:

Chronometer = (chronometer) Findviewbyid (r.id.chronometer);
Chronometer.setformat ("Time of Timing: (%s)");

This program will display the timer content of the above set, in addition, we can use the following several commonly used timer methods to control the timer work.
Copy Code code as follows:

A, Chronometer.start ()//Time to start
B, Chronometer.stop ()//Pause Timer
C, Chronometer.setbase (Systemclock.elapsedrealtime ());/reset timer, stop timing

Android is a control that allows us to easily implement timings in our programs, eliminating complex threads and redundant code, and saving a lot of development time.

the use of the Android timer (chronometer)
First step: Layout Files
The Main.xml code is as follows:
Copy Code code 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= "format"/>
</LinearLayout>
</LinearLayout>

Step Two: mainactivity
The code is as follows:
Copy Code code 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 Components
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 shock service
Chronometer = (chronometer) Findviewbyid (r.id.mychronometer);
Chronometer
. Setonchronometerticklistener (New Onchronometerticklistenerimpl ()); Set up a 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//timed listening event, change of monitoring time anytime and anywhere
Onchronometerticklistener {
@Override
public void Onchronometertick (chronometer chronometer) {
String time = Chronometer.gettext (). toString ();
if ("00:05". Equals (Time)) {//Judge five seconds later, let the phone vibrate
Vibrator.vibrate (New long[] {1000, 10, 100, 10}, 0);/set the vibration cycle and whether the cyclic vibration, if you do not want to circulate the vibration of 0 change 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 ("Show Time:%s."); /Change Time display format
Break
Default
Break
}
}
}
}

Step three: Register Permissions
Copy Code code as follows:

<uses-permission android:name= "Android.permission.VIBRATE"/>

You do not understand the world of God, the life of the worm you do not, only the process of the world of apes people know. Program apes, for their own wonderful World struggle bar, efforts! Come on......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.