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:运行结果:

Android Timer的使用的更多相关文章

  1. android Timer and TImerTask

    android Timer and TImerTask Caused by: java.lang.IllegalStateException: TimerTask is scheduled alrea ...

  2. Android Timer用法

    Android考虑到线程安全问题,不允许在线程中执行UI线程,在Android中,有一个类:android.os.Handler,这个可以实现各处线程间的消息传递.先看段代码,这个实例化了一个Hand ...

  3. Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》

    之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...

  4. Android Timer的应用示例

    package com.hyzhou.timerdemo1; import java.util.Timer; import java.util.TimerTask; import android.os ...

  5. android Timer TimerTask用法笔记

    Android中经常会遇到执行一些周期性定时执行的任务.初学的时候经常会使用Thread.sleep()方法.在android中,有Timer可以专门干这个事情. 先看看Timer.class中都是些 ...

  6. Android Timer 的 schedule()方法定时循环切换图片

    void java.util.Timer.schedule(TimerTask task, long delay, long period)第一个参数,是 TimerTask 类,在包:import ...

  7. android Timer使用方法

    Timer属性:http://www.apihome.cn/api/java/Timer.html 声明创建: private Timer mTimer; protected void onCreat ...

  8. android Timer与TimerTask的相关操作

    项目上面的部分操作需要使用到定时器进行周期性的控制.网络上面对于定时器的操作通常有三种实现方法. 我是通过Timer与TimerTask相结合实现的定时器功能.具体实现过程如下: 第一步,得到Time ...

  9. Xamarin.Android Timer使用方法

    首先声明命名空间: using System.Threading; 然后创建Timer对象: Timer timer; 接着实例化timer并且给委托事件: TimerCallback timerDe ...

随机推荐

  1. Hive从概念到安装使用总结

    一.Hive的基本概念 1.1 hive是什么? (1)Hive是建立在hadoop数据仓库基础之上的一个基础架构: (2)相当于hadoop之上的一个客户端,可以用来存储.查询和分析存储在hadoo ...

  2. XCode5/Apple LLVM 5.0下使用boost的方法

    Because Apple changes the compiler to llvm only in XCode5, so there are some compatible problems wit ...

  3. poj 3182 The Grove

    The Grove Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 641   Accepted: 297 Descripti ...

  4. [RxJS] Combining streams in RxJS

    Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...

  5. 编译lua版本问题

    Compile++ thumb  : game_shared <= main.cppjni/hellocpp/main.cpp: In function 'void Java_org_cocos ...

  6. Java基础知识强化30:String类之String的特点(String为什么是final)

    1. String字符串特点: 一旦被赋值,字符串值就不能改变. 这里String是final修饰的,具有不可继承性. 2. 为什么String是final? 主要是为了"效率"  ...

  7. ubuntu 切换JDK版本

    安装: 第一种方式:sudo apt-get install openjdk-7-jdk(or openjdk-6-jdk) 第二种方式:tar -zxvf jdk-7u79-linux-x64.ta ...

  8. HDU -1864最大报销额(01背包)

    这道题属于简单的01背包,但是背包问题还算简单,就是前面的细节处理的时候要注意,题意大致说了三条限制吧 1. 只有a, b, c 三种类型的发票可以报销,其它的一律不报销 2. 物品单项的报销额不超过 ...

  9. MyEclipse设置默认的文档注释

  10. 用timer控件实现sleep效果

    有时候我们需要代码延迟执行,这就需要用到Thread.Sleep()这个方法,但这个方法在主线程使用时会造成界面假死.使用timer控件既能达到代码延迟执行的效果,又不会有假死的困扰. 假设我们需要在 ...