欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 三种实现定时器详解及实现方法

程序员文章站 2024-03-06 10:06:19
方法一:handler+thread package com.xunfang.handerdemo; import android.app.activi...

方法一:handler+thread

package com.xunfang.handerdemo; 
 
import android.app.activity; 
import android.os.bundle; 
import android.os.handler; 
import android.os.message; 
import android.widget.textview; 
 
/** 
 * handler定时器 
 * 
 * @author smalt 
 * 
 */ 
public class handerdemoactivity extends activity { 
 textview tvshow; 
 private int i = 0; 
 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  tvshow = (textview) findviewbyid(r.id.tv_show); 
  new thread(new threadshow()).start(); 
 } 
 
 // handler类接收数据 
 handler handler = new handler() { 
  public void handlemessage(message msg) { 
   if (msg.what == 1) { 
    tvshow.settext(integer.tostring(i++)); 
    system.out.println("receive...."); 
   } 
  }; 
 }; 
 
 // 线程类 
 class threadshow implements runnable { 
 
  @override 
  public void run() { 
   // todo auto-generated method stub 
   while (true) { 
    try { 
     thread.sleep(1000); 
     message msg = new message(); 
     msg.what = 1; 
     handler.sendmessage(msg); 
     system.out.println("send..."); 
    } catch (exception e) { 
     // todo auto-generated catch block 
     e.printstacktrace(); 
     system.out.println("thread error..."); 
    } 
   } 
  } 
 } 
} 

方法二:handler类自带的postdelyed

package com.xunfang.handerdemo; 
 
import android.app.activity; 
import android.os.bundle; 
import android.os.handler; 
import android.widget.textview; 
 
/** 
 * handler定时器使用postdelyed实现 
 * 
 * @author smalt 
 * 
 */ 
public class handerdemoactivity extends activity { 
 textview tvshow; 
 private int i = 0; 
 private int time = 1000; 
 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  tvshow = (textview) findviewbyid(r.id.tv_show); 
  handler.postdelayed(runnable, time); //每隔1s执行 
 } 
 
 handler handler = new handler(); 
 runnable runnable = new runnable() { 
 
  @override 
  public void run() { 
   // handler自带方法实现定时器 
   try { 
    handler.postdelayed(this, time); 
    tvshow.settext(integer.tostring(i++)); 
    system.out.println("do..."); 
   } catch (exception e) { 
    // todo auto-generated catch block 
    e.printstacktrace(); 
    system.out.println("exception..."); 
   } 
  } 
 }; 
 
} 

方法三:handler+timer+timertask

package com.xunfang.handerdemo; 
 
import java.util.timer; 
import java.util.timertask; 
 
import android.app.activity; 
import android.os.bundle; 
import android.os.handler; 
import android.os.message; 
import android.widget.textview; 
 
/** 
 * 定时器实现:handler+timer+timertask 
 * 
 * @author smalt 
 * 
 */ 
public class handerdemoactivity extends activity { 
 textview tvshow; 
 private int i = 0; 
 private int time = 1000; 
 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  tvshow = (textview) findviewbyid(r.id.tv_show); 
  timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行 
 } 
 
 handler handler = new handler() { 
  public void handlemessage(message msg) { 
   if (msg.what == 1) { 
    tvshow.settext(integer.tostring(i++)); 
   } 
   super.handlemessage(msg); 
  }; 
 }; 
 timer timer = new timer(); 
 timertask task = new timertask() { 
 
  @override 
  public void run() { 
   // 需要做的事:发送消息 
   message message = new message(); 
   message.what = 1; 
   handler.sendmessage(message); 
  } 
 }; 
} 

以上就是对android 定时器的资料整理后续继续补充相关知识,谢谢大家对本站的支持!