package com.tuikes.timertest;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class TimerTestUI extends FragmentActivity {
public static final int MSG_WAY_ONE = 0x11;
public static final int MSG_WAY_TWO = 0x12;
public static final int MSG_WAY_THREE = 0x13;
private static final int WAY_FOUR_TOTAL_TIME_MILLIS = 60 * 1000; //默认60秒倒计时
@BindView(R.id.show_result_one)
TextView mResultOne;
@BindView(R.id.show_result_two)
TextView mResultTwo;
@BindView(R.id.show_result_three)
TextView mResultThree;
@BindView(R.id.show_result_four)
TextView mResultFour;
private Thread mWayOneThread;
private boolean mIsStartedWayOneThread = false; //用于控制方式一实现定时器的开启和关闭状态,onDestroy中需要将其置为false
private boolean mIsStartedWayTwoTimer = false; //用于控制方式二实现定时器的开启和关闭状态,onDestroy中需要将其置为false
private boolean mIsStartedWayThreeTimer = false; //用于取消方式四倒计时
private boolean mIsStartedWayFourTimer = false; //用于控制方式四实现定时器的开启和关闭状态
private CountDownTimer mCountDownTimer; //CountDownTimer是google封装的主要用于实现倒计时的
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
String text;
switch (msg.what) {
case MSG_WAY_ONE:
text = "方式一;" + msg.arg1;
mResultOne.setText(text);
break;
case MSG_WAY_TWO:
text = "方式二;" + msg.arg1;
mResultTwo.setText(text);
break;
case MSG_WAY_THREE:
text = "方式三;" + msg.arg1;
mResultThree.setText(text);
break;
}
return false;
}
});
private Timer mTimer = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*set it to be no title*/
requestWindowFeature(Window.FEATURE_NO_TITLE);
/*set it to be full screen*/
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_timer);
ButterKnife.bind(this);
}
@OnClick({R.id.way_one, R.id.way_two, R.id.way_three, R.id.way_four})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.way_one:
handleTimerOne();
break;
case R.id.way_two:
handleTimerTwo();
break;
case R.id.way_three:
handleTimerFour();
break;
case R.id.way_four:
handleDownTimer();
break;
}
}
private void handleTimerFour() {
if (mIsStartedWayThreeTimer) {
return;
}
mIsStartedWayThreeTimer = true;
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Message msg = new Message();
msg.what = MSG_WAY_THREE;
msg.arg1 = (int) (System.currentTimeMillis() / 1000);
mHandler.sendMessage(msg);
}
}, 0, 1000);
}
private void handleDownTimer() {
if (mCountDownTimer == null) {
mCountDownTimer = new CountDownTimer(WAY_FOUR_TOTAL_TIME_MILLIS, 1000) {//总共60秒,倒计时间隔为1秒
@Override
public void onTick(long millisUntilFinished) {
String text = "方式四;" + millisUntilFinished / 1000;
mResultFour.setText(text);
}
@Override
public void onFinish() {
//倒计时结束了
}
};
}
if (mIsStartedWayFourTimer) {
mIsStartedWayFourTimer = false;
mCountDownTimer.cancel();
} else {
mCountDownTimer.start();
mIsStartedWayFourTimer = true;
}
}
private void handleTimerTwo() {
if (mIsStartedWayTwoTimer) {
mIsStartedWayTwoTimer = false; //停止定时器
} else {
mIsStartedWayTwoTimer = true;
mHandler.postDelayed(mWayTwoRunnable, 0); //开始定时器
}
}
private void handleTimerOne() {
if (mWayOneThread == null) {
mWayOneThread = new MyThread();
}
if (!mIsStartedWayOneThread) { //开始定时器
mIsStartedWayOneThread = true;
mWayOneThread.start();
} else {
mIsStartedWayOneThread = false; //停止定时器
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//停止方式1定时器
mIsStartedWayOneThread = false;
//停止方式2定时器
mIsStartedWayTwoTimer = false;
mHandler.removeCallbacks(mWayTwoRunnable);
//停止方式3定时器
mTimer.cancel();
//停止方式4倒计时
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
}
private Runnable mWayTwoRunnable = new Runnable() {
@Override
public void run() {
if (mIsStartedWayTwoTimer) {
Message msg = new Message();
msg.what = MSG_WAY_TWO;
msg.arg1 = (int) (System.currentTimeMillis() / 1000);
mHandler.sendMessage(msg);
mHandler.postDelayed(mWayTwoRunnable, 1000);
}
}
};
private class MyThread extends Thread {
@Override
public void run() {
super.run();
if (mIsStartedWayOneThread) {
try {
Message msg = new Message();
msg.what = MSG_WAY_ONE;
msg.arg1 = (int) (System.currentTimeMillis() / 1000);
mHandler.sendMessage(msg);
Thread.sleep(1000);
mWayOneThread.start();
} catch (InterruptedException e) {
e.printStackTrace();
mIsStartedWayOneThread = false; //如果异常了,则停止倒计时
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android多种定时器实现
共31个文件
png:10个
xml:6个
java:3个
需积分: 15 8 下载量 13 浏览量
2017-10-10
17:56:08
上传
评论
收藏 132KB ZIP 举报
温馨提示
android实现定时器有很多种,例子中选了Timer、CountDownTimer、handler+postDelay、handler+handler等几种实现了一下,欢迎下载!
资源推荐
资源详情
资源评论
收起资源包目录
TimerSample.zip (31个子文件)
TimerTest
gradlew.bat 2KB
gradlew 5KB
gradle.properties 747B
app
proguard-rules.pro 968B
src
test
java
com
tuikes
timertest
ExampleUnitTest.java 414B
androidTest
java
com
tuikes
timertest
ExampleInstrumentedTest.java 770B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
drawable
bg_btn.xml 228B
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
values
colors.xml 214B
strings.xml 75B
styles.xml 374B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
layout
activity_timer.xml 5KB
java
com
tuikes
timertest
TimerTestUI.java 7KB
AndroidManifest.xml 737B
.gitignore 8B
build.gradle 1KB
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 236B
.gitignore 127B
local.properties 560B
settings.gradle 16B
build.gradle 521B
共 31 条
- 1
资源评论
伯兮
- 粉丝: 28
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功