賬号注冊是一個APP的首要任務,賬号注冊一般都要用到短信驗證,而短信驗證一般都需要一個60S倒計時,下面為大家分享一個倒計時服務!
首先自定義一個CountDownTimer,控制時長;
直接上代碼:
public class RegisterCodeTimer extends CountDownTimer {
private static Handler mHandler;
public static final int IN_RUNNING = 1001;
public static int END_RUNNING = 1002;
public long mMillisUntilFinished = 0;
/**
* @param millisInFuture // 倒計時的時長
* @param countDownInterval // 間隔時間
* @param handler // 通知進度的Handler
*/
public RegisterCodeTimer(long millisInFuture, long countDownInterval,
Handler handler) {
super(millisInFuture, countDownInterval);
mMillisUntilFinished = millisInFuture;
mHandler = handler;
}
// 結束
@Override
public void onFinish() {
mMillisUntilFinished = -1;
// TODO Auto-generated method stub
if (mHandler != null)
mHandler.obtainMessage(END_RUNNING, "重新發送").sendToTarget();
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
mMillisUntilFinished = millisUntilFinished;
if (mHandler != null)
mHandler.obtainMessage(IN_RUNNING,
(millisUntilFinished / 1000) + "s 後重新發送").sendToTarget();
}
}
接下來倒計時即便界面關閉了,倒計時依然要進行,是以需要通過Service來實作,自定義RegisterCodeTimerService,繼承Service;
代碼如下:
public class RegisterCodeTimerService extends Service {
private static int DEFAULT_TIME = 30000;
private static Intent mIntent;
private static Handler mHandler;
private static RegisterCodeTimer mCodeTimer;
private static View mSendCodeView;
/**
* 擷取用來倒計時的具體執行個體
* 就是一個線程
*
* @return
*/
public static RegisterCodeTimer getRegisterCodeTimer() {
return mCodeTimer;
}
public static void startService(Activity activity, int beginBackgroundRes) {
getIntent(activity);
mSendCodeView.setEnabled(false);
mSendCodeView.setBackgroundResource(beginBackgroundRes);
activity.startService(mIntent);
}
/**
* 擷取這個服務中的Intent
*
* @param context
* @return
*/
private static Intent getIntent(Context context) {
if (mIntent == null) {
mIntent = new Intent(context, RegisterCodeTimerService.class);
}
return mIntent;
}
/**
* 設定停止倒計時
*/
public static void onActivityFinish() {
if (mCodeTimer != null) {
mCodeTimer.cancel();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
mCodeTimer = new RegisterCodeTimer(DEFAULT_TIME, 1000, mHandler);
mCodeTimer.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
mCodeTimer.onFinish();
super.onDestroy();
}
/**
* 設定要進行倒計時的控件
*
* @param view 具體倒計時的控件
* @param endTimeBackgroundRes 結束倒計時後的背景資源
*/
public static void setCountDownView(View view, final int endTimeBackgroundRes) {
if (view instanceof TextView) {
final TextView textView = (TextView) view;
mSendCodeView = textView;
/**
* 倒計時Handler
*/
mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == RegisterCodeTimer.IN_RUNNING) {// 正在倒計時
textView.setText(msg.obj.toString());
textView.setBackgroundResource(R.drawable.bg_blue_fillet);
} else if (msg.what == RegisterCodeTimer.END_RUNNING) {// 完成倒計時
textView.setEnabled(true);
textView.setText(msg.obj.toString());
textView.setBackgroundResource(endTimeBackgroundRes);
}
}
};
}
}
/**
* 設定要倒計時的時間
*
* @param time
*/
public static void setCountDownTime(int time) {
DEFAULT_TIME = time;
}
}
然後就是使用了:
第一步:去清單檔案注冊Service;
<!-- 倒計時的Service -->
<service
android:name=".service.RegisterCodeTimerService"
android:enabled="true" />
第二步:設定要倒計時的控件,控件的背景色,倒計時時長;
RegisterCodeTimerService.setCountDownTime(60000);
RegisterCodeTimerService.setCountDownView(mDataBinding.registerBtnCode, R.drawable.bg_blue_fillet);
第三步:在需要開啟倒計時的地方,調用RegisterCodeTimerService中的startService方法,并設定倒計時開始控件的背景色;
RegisterCodeTimerService.startService(RegisterActivity.this, R.drawable.bg_blue_fillet);
第四步:當頁面消失的時候需要調用onActivityFinish();
@Override
protected void onDestroy() {
super.onDestroy();
RegisterCodeTimerService.onActivityFinish();
}
PS:一定不能忘了注冊Service哦;
[源碼下載下傳位址](https://github.com/lvqingfeng/Lightning/blob/master/app/src/main/java/com/lightningfast/service/RegisterCodeTimerService.java "源碼")