Android 自动刷新和倒计时刷新实现

Handler与Runnable实现间隔刷新,隔5s刷新一次;

 1 import android.app.Activity;
 2 import android.content.Context;
 3 import android.os.Bundle;
 4 import android.os.Handler;
 5 import android.widget.TextView;
 6 
 7 public class refreshActivity extends Activity {
 8     public String TAG = "LOG";
 9     private TextView vtockenstr;
10     String strtoken;
11     Context context;
12     Refreshpage resh = new Refreshpage();
13 
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.srtoken);
18         context = this;
19         vtockenstr = (TextView) findViewById(R.id.tokenstr);
20         vtockenstr.setText(strtoken);
21         resh.handler.postDelayed(resh.runnable, 10000);// 每兩秒執行一次runnable.
22     }
23 
24     @Override
25     public void onResume() {
26         // 回到Activity的时候重新开始刷新;
27         super.onResume();
28         resh.handler.postDelayed(resh.runnable, 10000);
29     }
30 
31     @Override
32     public void onPause() {
33         // 离开Activity的时候停止刷新;
34         super.onPause();
35         resh.handler.removeCallbacks(resh.runnable);
36     }
37 
38     @Override
39     public void onDestroy() {
40         super.onDestroy();
41         resh.handler.removeCallbacks(resh.runnable);
42     }
43 
44     class Refreshpage {
45         Handler handler = new Handler();
46         Runnable runnable = new Runnable() {
47             @Override
48             public void run() {
49                 // 要做的事情
50                 vtockenstr.setText(strtoken);
51                 handler.postDelayed(this, 10000);
52             }
53         };
54     }
55 
56 }
View Code

 

 CountDownTimer实现倒计时刷新,隔5分钟刷新一次;这里要注意的是Activity的生命周期问题,必须在onPause()和onResume() 方法里面停止和恢复进程,避免不必要的开销

  1 import java.text.SimpleDateFormat;
  2 import java.util.Date;
  3 import android.app.Activity;
  4 import android.content.Context;
  5 import android.content.SharedPreferences;
  6 import android.os.Bundle;
  7 import android.os.CountDownTimer;
  8 import android.util.Log;
  9 import android.view.View;
 10 import android.view.View.OnClickListener;
 11 import android.widget.Button;
 12 import android.widget.TextView;
 13 
 14 public class SRTokenActivity extends Activity {
 15     public String TAG = "LOG";
 16     private Button fixback;
 17     TextView tt;
 18     private TextView vtockenstr;
 19     String strtoken ;
 20     Context context;
 21     Date date;
 22     MyCount myCount;
 23     int minute,second,micount,allcount;
 24     String time;
 25     @Override
 26     public void onCreate(Bundle savedInstanceState) {
 27         Log.e(TAG, "start onCreate~~~"+time+"&"+allcount); 
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.srtoken);
 30         context = this;
 31         SharedPreferences sp = context.getSharedPreferences("serverparam",
 32                 MODE_PRIVATE); // 获得Preferences
 33         vtockenstr = (TextView)findViewById(R.id.tokenstr);
 34         vtockenstr.setText(strtoken);
 35         tt = (TextView)findViewById(R.id.countdowntime);
 36         fixback = (Button)findViewById(R.id.fixback);
 37         fixback.setOnClickListener(new OnClickListener(){
 38             @Override
 39             public void onClick(View v) {
 40                 SRTokenActivity.this.finish();
 41             }
 42         });
 43         vtockenstr.setText(strtoken);
 44         newtime();
 45         myCount.start();
 46         }
 47     
 48     //5分钟刷新一次,计算剩下的间隔时间
 49     public void newtime(){
 50         date = new Date(System.currentTimeMillis());
 51         SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
 52         time = sdf.format(date) ;
 53         minute = Integer.parseInt(time.substring(6,8));
 54         second = Integer.parseInt(time.substring(8,10));
 55         allcount = (4-minute%5)*60+(60-second);
 56         myCount = new MyCount(allcount*1000,1000);
 57         System.out.println("time:"+time);
 58     }
 59     
 60     
 61     //记录生命周期
 62     @Override  
 63     protected void onStart() {  
 64         super.onStart();  
 65         Log.e(TAG, "start onStart~~~"+time+"&"+allcount);  
 66     }  
 67     @Override  
 68     protected void onRestart() {  
 69         super.onRestart();  
 70         Log.e(TAG, "start onRestart~~~"+time+"&"+allcount); 
 71     }  
 72     @Override  
 73     protected void onResume() {  
 74         super.onResume();  
 75         Log.e(TAG, "start onResume~~~"+time+"&"+allcount); 
 76     }  
 77     @Override  
 78     protected void onPause() {  
 79         super.onPause();  
 80         Log.e(TAG, "start onPause~~~"+time+"&"+allcount); 
 81     }  
 82     @Override  
 83     protected void onStop() {  
 84         super.onStop();  
 85         Log.e(TAG, "start onStop~~~"+time+"&"+allcount); 
 86     }  
 87     @Override  
 88     protected void onDestroy() {  
 89         super.onDestroy();  
 90         Log.e(TAG, "start onDestroy~~~"+time+"&"+allcount); 
 91         myCount.cancel();
 92     }  
 93   
 94     class MyCount extends CountDownTimer {
 95         public MyCount(long millisInFuture, long countDownInterval) {
 96             super(millisInFuture, countDownInterval);
 97         }
 98         @Override
 99         public void onFinish() {
100             vtockenstr.setText(strtoken);
101             newtime();
102             myCount.start();
103         }
104         @Override
105         public void onTick(long millisUntilFinished) {
106             tt.setText("(" + millisUntilFinished / 1000 + ")秒后刷新...");
107         }
108         
109     }
110     
111 }
View Code

 

 

【转载】

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

http://blog.csdn.net/ithomer/article/details/6903084

CountDownTimer 实现倒计时

http://blog.csdn.net/yuanjh2001/article/details/6075315

其他例子

http://www.open-open.com/lib/view/open1343782266665.html

http://blog.csdn.net/chrp99/article/details/9169859

 

posted on 2013-11-05 12:15  Brown Qin  阅读(3703)  评论(0编辑  收藏  举报

导航