支付倒计时
来源:互联网 发布:jersey 返回对象json 编辑:程序博客网 时间:2024/05/16 11:41
在我们购物的时候会用到支付倒计时这个功能,其实Android已经帮封装好了一个类 CountDownTimer。
在一个TextView中不断显示剩下的时间,代码如下:
private TextView mTvSurplusTime;
private CountDownTimer timer = new CountDownTimer(20*60*1000,1000) {
@Override
public void onTick(long millisUntilFinished) {
long minute = millisUntilFinished/(60*1000);
long second=(millisUntilFinished-minute*60*1000)/1000;
String convertTime=minute+"分"+second+"秒";
mTvSurplusTime.setText("支付剩余时间"+convertTime);
}
@Override
public void onFinish() {
Util.showToast("20分钟已到,请重新下单",mContext);
finish();
}
};
调用的时候很简单:timer.start();
在CountDownTimer timer = new CountDownTimer(20*60*1000,1000)中,第一个参数表示总时间,第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick,然后20分钟之后会回调onFinish方法。
下面是CountDownTimer的源码,从源码可以发现,CountDownTimer也是基于Handler进行处理的:
packageandroid.os;
/**
* Schedule a countdown until a time in the future, with
* regular notifications on intervals along the way.
*
* Example of showing a 30 second countdown in a text field:
*
*
* new CountDownTimer(30000, 1000) {
*
* public void onTick(long millisUntilFinished) {
* mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
* }
*
* public void onFinish() {
* mTextField.setText("done!");
* }
* }.start();
*
*
* The calls to {@link#onTick(long)} are synchronized to this object so that
* one call to {@link#onTick(long)} won't ever occur before the previous
* callback is complete. This is only relevant when the implementation of
* {@link#onTick(long)} takes an amount of time to execute that is significant
* compared to the countdown interval.
*/
public abstract classCountDownTimer{
/**
* Millis since epoch when alarm should stop.
*/
private final longmMillisInFuture;
/**
* The interval in millis that the user receives callbacks
*/
private final longmCountdownInterval;
private longmStopTimeInFuture;
/**
* boolean representing if the timer was cancelled
*/
private booleanmCancelled=false;
/**
*@param millisInFutureThe number of millis in the future from the call
* to {@link#start()} until the countdown is done and {@link#onFinish()}
* is called.
*@param countDownIntervalThe interval along the way to receive
* {@link#onTick(long)} callbacks.
*/
publicCountDownTimer(longmillisInFuture,longcountDownInterval) {
mMillisInFuture=millisInFuture;
mCountdownInterval=countDownInterval;
}
/**
* Cancel the countdown.
*/
public synchronized final voidcancel() {
mCancelled=true;
mHandler.removeMessages(MSG);
}
/**
* Start the countdown.
*/
public synchronized finalCountDownTimerstart() {
mCancelled=false;
if(mMillisInFuture<=0) {
onFinish();
return this;
}
mStopTimeInFuture=SystemClock.elapsedRealtime()+mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
/**
* Callback fired on regular interval.
*@param millisUntilFinishedThe amount of time until finished.
*/
public abstract voidonTick(longmillisUntilFinished);
/**
* Callback fired when the time is up.
*/
public abstract voidonFinish();
private static final intMSG=1;
// handles counting down
privateHandlermHandler=newHandler() {
@Override
public voidhandleMessage(Messagemsg) {
synchronized(CountDownTimer.this) {
if(mCancelled) {
return;
}
final longmillisLeft=mStopTimeInFuture-SystemClock.elapsedRealtime();
if(millisLeft<=0) {
onFinish();
}else if(millisLeft
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG),millisLeft);
}else{
longlastTickStart=SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
longdelay=lastTickStart+mCountdownInterval-SystemClock.elapsedRealtime();
// special case: user's onTick took more than interval to
// complete, skip to next interval
while(delay<0)delay+=mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG),delay);
}
}
}
};
}
- 支付倒计时
- 微信小程序-支付倒计时
- JS制作支付倒计时页面
- 使用AngularJS写一个支付倒计时
- 支付倒计时与圆环波纹动画效果
- 百度C2C支付平台定名百付宝 上线进入倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时、、、、、
- 倒计时
- 倒计时
- SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群
- 错误1:malloc.c:2394
- Tensorflow: 保存和复原模型(save and restore)
- vue中npm run dev运行项目不能自动打开浏览器!
- Python数据抓取(2) —简单网络爬虫的撰写
- 支付倒计时
- 数据结构第五周项目-括号的匹配
- Python实现的检测web服务器健康状况的小程序
- WebViewJavascriptBridge是一个Objective-C与JavaScript进行消息互通的三方库
- 【TensorFlow】数据处理(对图像的处理)
- 链式调用-普通调用
- React Native 学习资源精选仓库(汇聚知识,分享精华)
- SimpleDateFormat的使用
- HbaseTemplate配置进阶:利用Spring自动装配加载HbaseTemplate