最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Timer使用的實(shí)例代碼

 更新時(shí)間:2013年10月15日 16:57:01   作者:  
這篇文章介紹了Android Timer使用的實(shí)例代碼,有需要的朋友可以參考一下

1:服務(wù)端使用PHP

復(fù)制代碼 代碼如下:

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

2:activity_main.xml

復(fù)制代碼 代碼如下:

<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://m.fzitv.net/article/42126.htm

復(fù)制代碼 代碼如下:

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:運(yùn)行結(jié)果:

相關(guān)文章

最新評(píng)論

泊头市| 博乐市| 色达县| 正蓝旗| 札达县| 八宿县| 田东县| 闽侯县| 贵溪市| 含山县| 仪陇县| 胶州市| 徐闻县| 资阳市| 淄博市| 银川市| 当雄县| 都安| 正安县| 新余市| 莱西市| 措美县| 巴彦县| 永寿县| 屯留县| 宝丰县| 柳河县| 嘉义市| 嘉禾县| 毕节市| 枞阳县| 博客| 内丘县| 九江县| 金溪县| 广西| 饶平县| 石屏县| 富顺县| 榆中县| 阿城市|