android实现计时与倒计时的几种方法

asu 贡献于2014-05-07

作者   创建于2012-06-06 01:30:27   修改者Administrator  修改于2014-04-20 14:24:12字数3861

文档摘要:android实现计时与倒计时的几种方法
关键词:

 Android实现计时与倒计时的几种方法 方法一 Timer与TimerTask(Java实现) [java] view plaincopyprint? 1 public class timerTask extends Activity{ 2 3 private int recLen = 11; 4 private TextView txtView; 5 Timer timer = new Timer(); 6 7 public void onCreate(Bundle savedInstanceState){ 8 super.onCreate(savedInstanceState); 9 10 setContentView(R.layout.timertask); 11 txtView = (TextView)findViewById(R.id.txttime); 12 13 timer.schedule(task, 1000, 1000); // timeTask 14 } 15 16 TimerTask task = new TimerTask() { 17 @Override 18 public void run() { 19 20 runOnUiThread(new Runnable() { // UI thread 21 @Override 22 public void run() { 23 recLen--; 24 txtView.setText(""+recLen); 25 if(recLen < 0){ 26 timer.cancel(); 27 txtView.setVisibility(View.GONE); 28 } 29 } 30 }); 31 } 32 }; 33 } 方法二 TimerTask与Handler(不用Timer的改进型) [java] view plaincopyprint? 34 public class timerTask extends Activity{ 35 private int recLen = 11; 36 private TextView txtView; 37 Timer timer = new Timer(); 38 39 public void onCreate(Bundle savedInstanceState){ 40 super.onCreate(savedInstanceState); 41 42 setContentView(R.layout.timertask); 43 txtView = (TextView)findViewById(R.id.txttime); 44 45 timer.schedule(task, 1000, 1000); // timeTask 46 } 47 48 final Handler handler = new Handler(){ 49 @Override 50 public void handleMessage(Message msg){ 51 switch (msg.what) { 52 case 1: 53 txtView.setText(""+recLen); 54 if(recLen < 0){ 55 timer.cancel(); 56 txtView.setVisibility(View.GONE); 57 } 58 } 59 } 60 }; 61 62 TimerTask task = new TimerTask() { 63 @Override 64 public void run() { 65 recLen--; 66 Message message = new Message(); 67 message.what = 1; 68 handler.sendMessage(message); 69 } 70 }; 71 } 方法三 Handler与Message(不用TimerTask) [java] view plaincopyprint? 72 public class timerTask extends Activity{ 73 private int recLen = 11; 74 private TextView txtView; 75 76 public void onCreate(Bundle savedInstanceState) { 77 super.onCreate(savedInstanceState); 78 79 setContentView(R.layout.timertask); 80 txtView = (TextView)findViewById(R.id.txttime); 81 82 Message message = handler.obtainMessage(1); // Message 83 handler.sendMessageDelayed(message, 1000); 84 } 85 86 final Handler handler = new Handler(){ 87 88 public void handleMessage(Message msg){ // handle message 89 switch (msg.what) { 90 case 1: 91 recLen--; 92 txtView.setText("" + recLen); 93 94 if(recLen > 0){ 95 Message message = handler.obtainMessage(1); 96 handler.sendMessageDelayed(message, 1000); // send message 97 }else{ 98 txtView.setVisibility(View.GONE); 99 } 100 } 101 102 super.handleMessage(msg); 103 } 104 }; 105 } 方法四 Handler与Thread(不占用UI线程) [java] view plaincopyprint? 106 public class timerTask extends Activity{ 107 private int recLen = 0; 108 private TextView txtView; 109 110 public void onCreate(Bundle savedInstanceState){ 111 super.onCreate(savedInstanceState); 112 113 setContentView(R.layout.timertask); 114 txtView = (TextView)findViewById(R.id.txttime); 115 116 new Thread(new MyThread()).start(); // start thread 117 } 118 119 final Handler handler = new Handler(){ // handle 120 public void handleMessage(Message msg){ 121 switch (msg.what) { 122 case 1: 123 recLen++; 124 txtView.setText("" + recLen); 125 } 126 super.handleMessage(msg); 127 } 128 }; 129 130 public class MyThread implements Runnable{ // thread 131 @Override 132 public void run(){ 133 while(true){ 134 try{ 135 Thread.sleep(1000); // sleep 1000ms 136 Message message = new Message(); 137 message.what = 1; 138 handler.sendMessage(message); 139 }catch (Exception e) { 140 } 141 } 142 } 143 } 144 } 方法五 Handler与Runnable(最简单型) [java] view plaincopyprint? 145 public class timerTask extends Activity{ 146 private int recLen = 0; 147 private TextView txtView; 148 149 public void onCreate(Bundle savedInstanceState){ 150 super.onCreate(savedInstanceState); 151 152 setContentView(R.layout.timertask); 153 txtView = (TextView)findViewById(R.id.txttime); 154 155 handler.postDelayed(runnable, 1000); 156 } 157 158 Handler handler = new Handler(); 159 Runnable runnable = new Runnable() { 160 @Override 161 public void run() { 162 recLen++; 163 txtView.setText("" + recLen); 164 handler.postDelayed(this, 1000); 165 } 166 }; 167 } 计时与倒计时 方法1,方法2和方法3,都是倒计时 方法4,方法5,都是计时 计时和倒计时,都可使用上述方法实现(代码稍加改动) UI线程比较 方法1,方法2和方法3,都是在UI线程实现的计时; 方法4和方法5,是另开Runnable线程实现计时 实现方式比较 方法1,采用的是Java实现,即Timer和TimerTask方式; 其它四种方法,都采用了Handler消息处理 推荐使用 如果对UI线程交互要求不很高,可以选择方法2和方法3 如果考虑到UI线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理 方法5,综合了前几种方法的优点,是最简的

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 2 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档