Instance code used by the Android timer _android

Source: Internet
Author: User
Tags echo date

1: The service side uses PHP

Copy Code code as follows:

<?php
echo Date (' y-m-d h:i:s ');
?>

2:activity_main.xml

Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<button
Android:id= "@+id/btn_click"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "button Start"/>

<textview
Android:id= "@+id/tv_show"
android:layout_below= "@id/btn_click"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"
Android:textsize= "28sp"/>

<button
Android:id= "@+id/btn_stop"
android:layout_below= "@id/tv_show"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "button Stop"/>

</RelativeLayout>

3:mainactivity.java

Httphelper.getstringfromnet2 (param) This method is shown in: http://www.jb51.net/article/42126.htm

Copy Code code as follows:

public class Mainactivity extends activity {
Private Button Btnclick=null;
Private Button Btnstop=null;
Private TextView Tvshow=null;
Private String info= "";
Private Timer Timer=null;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

btnclick= (Button) Findviewbyid (R.id.btn_click);
btnstop= (Button) Findviewbyid (r.id.btn_stop);
tvshow= (TextView) Findviewbyid (r.id.tv_show);

Timer=new Timer ();

Btnclick.setonclicklistener (New Onclicklistener () {
public void OnClick (view view) {
Timer.scheduleatfixedrate (New MyTask (), 100, 2000);
}
});

Btnstop.setonclicklistener (New Onclicklistener () {
public void OnClick (view view) {
Timer.cancel ();
}
});

}

Handler myhandler=new Handler () {
public void Handlemessage (msg) {
if (info!= "") {
Tvshow.settext (info);
}
}
};

Private class MyTask extends timertask{
public void Run () {
String param= "http://192.168.0.116/android/time.php";
Info=httphelper.getstringfromnet2 (param);
Myhandler.obtainmessage. Sendtotarget ();
}
}
}

4: Running Result:

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.