Android Timer chronometer

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 16px; font-family: ‘times new roman‘, times;">The timer chronometer inherits from TextView and shows how long it has been since a start time.</span></p></p><p><p><span style="font-size: 16px; font-family: ‘times new roman‘, times;">Property has only one format, which specifies the timing format of the Timer.</span></p></p><p><p><span style="font-size: 16px; font-family: ‘times new roman‘, times;">To use the time interval, you can use Systemclock to Achieve:</span></p></p><pre><pre><span style="font-size: 16px; font-family: ‘times new roman‘, times;">1.systemclock.currentthreadtimemillis (); Elapsed time in the current thread</span><br><span style="font-size: 16px; font-family: ‘times new roman‘, times;">2.systemclock.elapsedrealtime (); Milliseconds from power on to now (sleep) is also included in the time of the phone</span><br><span style="font-size: 16px; font-family: ‘times new roman‘, times;">3.systemclock.uptimemillis (); Milliseconds from power on to now (phone sleep time not included)</span><br><span style="font-size: 16px; font-family: ‘times new roman‘, times;">4.systemclock.sleep (100); Similar to Thread.Sleep (100); but the method ignores interruptedexception</span><br><span style="font-size: 16px; font-family: ‘times new roman‘, times;">5.systemclock.setcurrenttimemillis (1000); Set the clock time, similar to System.setcurrenttimemillis</span></pre></pre><p><p><span style="font-size: 16px; font-family: ‘times new roman‘, times;">Code:</span></p></p><p><p><span style="font-size: 16px; font-family: ‘times new roman‘, times;">Main_activity.xml</span></p></p><pre><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"><?xml version= "1.0" encoding= "utf-8"?></span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:layout_width= "match_parent"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:layout_height= "match_parent"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:orientation= "vertical"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:gravity= "center_horizontal" ></span></span><br><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"><chronometer</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:id= "@+id/test"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:layout_height= "wrap_content"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:layout_width= "wrap_content"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:textsize= "12pt"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:textcolor= "#ffff0000"/></span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"><button</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:layout_width= "wrap_content"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:layout_height= "wrap_content"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:id= "@+id/start"</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">android:text= "start"/></span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"></LinearLayout></span></span><br><span style="font-size: 16px; font-family: ‘times new roman‘, times;"><span style="font-size: 16px; font-family: ‘times new roman‘, times;">Mainactivity.java<br></span></span></pre><pre><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Import android.app.Activity;</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Import android.os.*;</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Import android.view.View;</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Import android.widget.*;</span></span><br><br><span style="font-family: ‘times new roman‘, times;">public <span style="font-family: ‘times new roman‘, times;">class Mainactivity extends Activity {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">chronometer ch;</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Button start;</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">@Override</span></span><br><span style="font-family: ‘times new roman‘, times;">public <span style="font-family: ‘times new roman‘, times;">void OnCreate (Bundle Savedinstancestate) {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Super.oncreate (savedinstancestate);//call The OnCreate constructor of the parent class, savedinstancestate save state information for the current Activity. Each activity is required to initiate some necessary initialization, which is done by invoking the constructor of the parent class, for example, to generate a blank activity, at least a caption, how the title is associated and generated, and is handled by a constructor Function. </span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Setcontentview (r.layout.activity_main);</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">ch= (chronometer) Findviewbyid (r.id.test);</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">start= (Button) Findviewbyid (r.id.start);</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Start.setonclicklistener (new view.onclicklistener () {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">@Override</span></span><br><span style="font-family: ‘times new roman‘, times;">public <span style="font-family: ‘times new roman‘, times;">void OnClick (View v) {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">ch.setbase (systemclock.elapsedrealtime ());//systemclock.elapsedrealtime () number of milliseconds from power-on to present (phone sleep time Included)</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Ch.start ();</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">start.setenabled (false);</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"> }</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"> });</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">Ch.setonchronometerticklistener (new chronometer.onchronometerticklistener () {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">@Override</span></span><br><span style="font-family: ‘times new roman‘, times;">public <span style="font-family: ‘times new roman‘, times;">void Onchronometertick (chronometer Ch) {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">if (systemclock.elapsedrealtime ()-ch.getbase () >30*1000) {</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">ch.stop ();</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">start.setenabled (true);</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"> }</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"> }</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"> });</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;"> }</span></span><br><span style="font-family: ‘times new roman‘, times;"><span style="font-family: ‘times new roman‘, times;">}</span></span></pre><pre><pre><span style="font-size: 16px; font-family: 宋体;">Operation Result:</span></pre></pre><p><p></p></p><pre><pre></pre></pre><p><p>Android Timer chronometer</p></p></span>

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.