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: