转载请标明出处:http://blog.csdn.net/nongminkouhao/article/details/52797001
方法1:Timer TimeTask(java)
//监控程序
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
//执行你需要的定时任务
}
};
timer.schedule(task, 0, 3000);//每3s执行run方法
方法2:Handler(android)
//通过handler实现轮播图滚动
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//获取当前的位置
int currentItem = mViewPager.getCurrentItem();
//如果当前的page是最后一个,就让它自动跳到第一个,否则就跳到后面一个
if(currentItem==mViewPager.getAdapter().getCount()-1){
mViewPager.setCurrentItem(0);
}else{
currentItem = currentItem+1;
mViewPager.setCurrentItem(currentItem);
}
//因为postDelay是在2000后执行run方法,只执行一次
//多次执行
handler.postDelayed(this,2000);
}
}, 2000);
方法3:CoutDownTimer(android)
使用这个发现了跳秒的情况,打印log发现,如果执行单位为1000ms,打印出来的每次并不是减少1000ms,有可能多有可能少
所以在这里使用500ms进行判断,里面做了逻辑处理
工具类:
package hj.countdowntimer;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;
/**
* 功能:发送短信验证码倒计时
* @author xiehj
* @time 2016/10/10 14:05
*/
public class SendSmsTimerUtils extends CountDownTimer {
private int inFuture;//按下之前背景颜色
private int downInterval;//按下之后背景颜色
private int countTemp = 2;//由于以1秒间隔不准,所以取其一半来做间隔
private TextView mTextView;
public SendSmsTimerUtils(TextView textView, long millisInFuture, long countDownInterval, int inFuture, int downInterval) {
super(millisInFuture, countDownInterval/2);
this.mTextView = textView;
this.inFuture=inFuture;
this.downInterval=downInterval;
}
public void onTick(long millisUntilFinished) {
mTextView.setClickable(false);
mTextView.setBackgroundResource(downInterval);
if (countTemp == 2) {
//可以看出来其实millis并不是非常标准的
Log.w("-------------------", String.format("%d毫秒", millisUntilFinished));
Log.w("-------------------", String.format("%d秒", Math.round(millisUntilFinished / 1000.0)));
SpannableString spannableString = new SpannableString(String.format("%d秒后可重新发送", Math.round(millisUntilFinished / 1000.0)));
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
//设置秒数为红色
if (Math.round(millisUntilFinished / 1000.0) > 9) {
spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
} else {
spannableString.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
mTextView.setText(spannableString);
countTemp--;
} else {
countTemp++;
}
}
@Override
public void onFinish() {
countTemp = 2;
mTextView.setText("重新获取验证码");
mTextView.setClickable(true);
mTextView.setBackgroundResource(inFuture);
}
}
使用方法
package hj.countdowntimer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_send_sms;
private CountDownTimer countDownTimer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
}
private void initView() {
tv_send_sms = (TextView) findViewById(R.id.tv_send_sms);
countDownTimer = new SendSmsTimerUtils(tv_send_sms,60000,1000,R.color.colorAccent,R.color.colorPrimaryDark);
}
private void initEvent() {
tv_send_sms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
countDownTimer.start();
}
});
}
}
参考文章: