Android Timer的使用
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的使用的更多相关文章
- android Timer and TImerTask
android Timer and TImerTask Caused by: java.lang.IllegalStateException: TimerTask is scheduled alrea ...
- Android Timer用法
Android考虑到线程安全问题,不允许在线程中执行UI线程,在Android中,有一个类:android.os.Handler,这个可以实现各处线程间的消息传递.先看段代码,这个实例化了一个Hand ...
- Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》
之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...
- Android Timer的应用示例
package com.hyzhou.timerdemo1; import java.util.Timer; import java.util.TimerTask; import android.os ...
- android Timer TimerTask用法笔记
Android中经常会遇到执行一些周期性定时执行的任务.初学的时候经常会使用Thread.sleep()方法.在android中,有Timer可以专门干这个事情. 先看看Timer.class中都是些 ...
- Android Timer 的 schedule()方法定时循环切换图片
void java.util.Timer.schedule(TimerTask task, long delay, long period)第一个参数,是 TimerTask 类,在包:import ...
- android Timer使用方法
Timer属性:http://www.apihome.cn/api/java/Timer.html 声明创建: private Timer mTimer; protected void onCreat ...
- android Timer与TimerTask的相关操作
项目上面的部分操作需要使用到定时器进行周期性的控制.网络上面对于定时器的操作通常有三种实现方法. 我是通过Timer与TimerTask相结合实现的定时器功能.具体实现过程如下: 第一步,得到Time ...
- Xamarin.Android Timer使用方法
首先声明命名空间: using System.Threading; 然后创建Timer对象: Timer timer; 接着实例化timer并且给委托事件: TimerCallback timerDe ...
随机推荐
- in, out, ref
C#中的函数传递方式可以为in.out.ref(引用) in方式的是默认的传递方式,即向函数内部传送值,不作讲解 很多语言都有类似的操作从函数向调用者返回值,这样我们可以通过函数的调用返回多个值,因为 ...
- js的简单模板解析
在编程中总是会遇见很多动态生成的东西,一般我们都是通过简单的html拼接起来的 function createHtml(name, phone, addr, email, imageSrc){ var ...
- solr + tomcat 搭建
1.准备jdk7和tomcat72.拷贝solr目录下example/webapps/solr.war,到tomcat下的webapps目录中.3.启动tomcat74.编辑tomcat7中的weba ...
- asp.net using library ClosedXML to export excel
Reference: http://closedxml.codeplex.com/ 1. First add refenrence ClosedXML.dll and DocumentFormat. ...
- hdu 2844
本题的特殊性是价值与重量相等 将第i种物品分成若干件物品,其中每件物品有一个系数, 这件物品的费用和价值均是原来的费用和价值乘以这个系数. 使这些系数分别为1,2,4,…,2^(k-1),n[i]-2 ...
- 《火球——UML大战需求分析》(第1章 大话UML)——1.4 如何学好UML?
说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...
- Tomcat jdbc pool配置
Tomcat jdbc pool是apache在tomcat7版本中启用的新连接池,用它来解决以往DBCP无法解决的一些问题. Tomcat jdbc pool的优点: (1) tomcat j ...
- C#App.config的使用
为什么使用App.config, 在连接数据的时候将连接字符串写在了类中,如果更换数据库地址,则需要修改这个类,然后重新编译才可以重新连接数据库.在这个时候我们就可以将连接信息放到配置文件App.co ...
- asp.net 连接oracle,报错误“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
1.http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 下载对用版本的Instan ...
- 马士兵SVN.
下载 服务端:VisualSVN Server 和客户端:TortoiseSVN cmd,并cd 到 VisualSVN Server安装目录下的bin目录. 新建库: svnadmin create ...