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 ...
随机推荐
- Selenium IDE整理
安装 Step1: 下载Firefox浏览器 http://www.firefox.com.cn/ Step2: 安装Selenium IDE插件 http://seleniumhq.org/down ...
- CloudFoundry云环境中应用的特殊设计
常规的应用,大多数可以不经过任何修改即可部署于CloudFoundry云平台之上,但是在一些特殊情况下,总是不可避免地会出现一些细小的问题,如果在应用设计之初,就考虑到针对云平台的一些特殊情况,遵守云 ...
- jQuery(二)
jQuery学完了!好用! 1.用定时器做的jquery里面的animate效果 <!DOCTYPE html> <html lang="en"> < ...
- 《使用Win32DiskImager安装Ubuntu16.04》
之前,一直使用的是“软碟通”来制作Ubuntu启动盘,或使用“EasyBCD”硬盘安装双系统,但前者在Ubuntu14.10及以后版本中便再也行不通了.此问题是由于Ubuntu使用了最新的syslin ...
- 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing
git提交代码时,出现这个错误"error: The requested URL returned error: 403 Forbidden while accessing https&qu ...
- Android BaseAdapter ListView (明星简介列表)
1.搭建布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- VS2010 使用TeeChart画图控件 - 之二 - 绘制图形(折线图,柱状图)
1.前期准备 详细可见VS2010 使用TeeChart画图控件 - 之中的一个 控件和类的导入 1. 1 加入TeeChart控件,给控件加入变量m_TeeChart 加入TeeChart控件,右击 ...
- 单向链表JAVA代码
//单向链表类 publicclassLinkList{ //结点类 publicclassNode{ publicObject data; ...
- java集合框架03
Collections工具类的使用 public class News implements Comparable { private int id; //新闻编号 private String ti ...
- 秒味课堂Angular js笔记------Angular js中的工具方法
Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...