Android Alarm Clock (three): realize the clock function

Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>Timeview.java</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.mytest.myclock;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Calendar;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.Context;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.opengl.Visibility;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Handler;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.util.AttributeSet;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.LinearLayout;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.TextView;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Timeview<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">LinearLayout {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">TextView timetext; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Timeview (context context, AttributeSet attrs,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Defstyle) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context, attrs, defstyle); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Constructor stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">timeview (context context, attributeset attrs) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context, attrs); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Constructor stub</span></span><span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Timeview (context Context) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Constructor stub</span></span><span style="color: #000000;"><span style="color: #000000;">} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onfinishinflate () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Onfinishinflate (); Timetext</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.tv_time); Timehandler.sendemptymessage (</span></span>0<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">refreshtime () {Calendar cal</span></span>=<span style="color: #000000;"><span style="color: #000000;">calendar.getinstance (); String Timestr</span></span>= String.Format ("%d:%d:%d"<span style="color: #000000;"><span style="color: #000000;">, Cal.get (calendar.hour_of_day), cal.get (calendar.minute), cal.get (calendar.second)); Timetext.settext (timestr); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">System.out.println ("timestr============>" + timestr);</span></span><span style="color: #000000;"><span style="color: #000000;">} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onvisibilitychanged (View changedview,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Visibility) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. onvisibilitychanged (changedview, visibility); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(getvisibility () = =<span style="color: #000000;"><span style="color: #000000;">View.visible) {timehandler.sendemptymessagedelayed (</span></span>0, 1000<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{timehandler.removemessages (</span></span>0<span style="color: #000000;"><span style="color: #000000;">); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Handler Timehandler =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Handler () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">handlemessage (android.os.Message msg) {refreshtime (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(getvisibility () = =<span style="color: #000000;"><span style="color: #000000;">View.visible) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">send a message to yourself every second</span></span>timehandler.sendemptymessagedelayed (0, 1000<span style="color: #000000;"><span style="color: #000000;">); } }; };}</span></span></pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">View Code</span></span><p><p></p></p><p><p>Summary:</p></p><p><p>1, send the message in onfinishinflate, realize the first time display. later, the message is sent in the handler Handlemessage method, which is somewhat equivalent to invoking a dead loop to refresh the Time.</p></p><p><p>2, when the user switch, the Clock tab is not visible, remember to stop sending messages in onvisibilitychanged, no longer refresh Time.</p></p><p><p>Android Alarm Clock (three): realize the clock function</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.