1:服务端使用PHP

<?php    echo date('Y-m-d H:i:s');?>

2:activity_main.xml

<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)此方法见:http://www.cnblogs.com/yshyee/p/3370147.html

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(Message 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(100).sendToTarget();        }    }}

4:运行结果:

更多相关文章

  1. android studio 3.1 Android(安卓)Device Monitor 新的启动方式
  2. opencv for android:如何在Android(安卓)studio中成功运行opencv
  3. android之NDK(jni)开发笔记1——运行第一个NDK程序
  4. Android从启动到程序运行发生的事情
  5. 【Android增量升级系列_02】 浅谈Android增量更新服务端的实现方
  6. Frida官方手册 - 在Android上使用Frida
  7. android获得密钥
  8. 【Android】IPC(进程间通信)
  9. 项目运行报错Error: Static interface methods are only support

随机推荐

  1. android edittext 输入手机号码格式变化
  2. android猜数字游戏
  3. Android之数据库
  4. Android(安卓)中获取屏幕长度及宽度
  5. Android如何截取当前View 为图片
  6. Android与服务器传递数据
  7. android 界面 滑入 效果
  8. android-数据库操作实例,留着以后用
  9. android 的popwindow弹窗
  10. android计算器简单版【安卓进化一】