我一直在经历很多问题尝试暂停和取消暂停计时器,如果我将方向锁定为纵向或横向它可以工作,但这不完全是我想要做的.当然,当您更改方向时会调用onCreate方法,因此我取消了我的timertask并将其设置为null,但是在多次运行方向后,它不再取消时间任务.我在这里查看了其他人的问题,但似乎没有人能够回答我的问题.继承我的代码.它现在有点草率,因为我一直在尝试我能做的一切.

public class singleTimer extends Activity implements OnClickListener {

private Integer setTime = 0;

private Integer tmrSeconds = 0;

private Integer tmrMilliSeconds = 0;

private Timer myTimer = new Timer();

private TimerTask myTimerTask;

private TextView timerText;

private boolean isPaused = true;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.single_timer);

Bundle extras = getIntent().getExtras();

setTime = extras.getInt("com.bv.armyprt.timer_duration");

if (myTimerTask != null) {

myTimerTask.cancel();

myTimerTask = null;

}

if (savedInstanceState != null) {

if (savedInstanceState.getInt("tmrSeconds") == 0) {

tmrSeconds = setTime;

} else {

tmrSeconds = savedInstanceState.getInt("tmrSeconds");

tmrMilliSeconds = savedInstanceState.getInt("tmrMilliseconds");

if (isPaused == false) {

myTimer = new Timer();

myTimerTask = new TimerTask() {

@Override

public void run() {

TimerMethod();

}

};

myTimer.schedule(myTimerTask, 0, 100);

}

}

} else {

tmrSeconds = setTime;

}

timerText = (TextView)findViewById(R.id.timerText);

timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));

TextView timerDesc = (TextView)findViewById(R.id.timerDescription);

timerDesc.setText("Timer for: " + setTime.toString());

Button startButton = (Button)findViewById(R.id.timerStart);

Button stopButton = (Button)findViewById(R.id.timerStop);

Button closeButton = (Button)findViewById(R.id.timerClose);

closeButton.setOnClickListener(this);

startButton.setOnClickListener(this);

stopButton.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case (R.id.timerStart):

isPaused = false;

myTimer = new Timer();

myTimerTask = new TimerTask() {

@Override

public void run() {

TimerMethod();

}

};

myTimer.schedule(myTimerTask,0, 100);

break;

case (R.id.timerStop):

isPaused = true;

myTimerTask.cancel();

myTimerTask = null;

myTimer.cancel();

break;

case (R.id.timerClose):

onDestroy();

this.finish();

break;

}

}

private void TimerMethod()

{

//This method is called directly by the timer

//and runs in the same thread as the timer.

//We call the method that will work with the UI

//through the runOnUiThread method.

this.

tmrMilliSeconds--;

this.runOnUiThread(Timer_Tick);

}

private Runnable Timer_Tick = new Runnable() {

public void run() {

//This method runs in the same thread as the UI.

if (tmrSeconds > 0) {

if (tmrMilliSeconds <= 0) {

tmrSeconds--;

tmrMilliSeconds = 9;

}

} else {

Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

v.vibrate(1000);

myTimer.cancel();

tmrSeconds = setTime;

tmrMilliSeconds = 0;

isPaused = true;

}

//Do something to the UI thread here

timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));

}

};

@Override

public void onSaveInstanceState(Bundle savedInstanceState){

savedInstanceState.putInt("setTimer", setTime);

savedInstanceState.putInt("tmrSeconds", tmrSeconds);

savedInstanceState.putInt("tmrMilliseconds", tmrMilliSeconds);

super.onSaveInstanceState(savedInstanceState);

}

@Override

public void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);

setTime = savedInstanceState.getInt("setTimer");

tmrSeconds = savedInstanceState.getInt("tmrSeconds");

tmrMilliSeconds = savedInstanceState.getInt("tmrMilliSeconds");

}

}

android如何暂停倒计时,Android – 如何停止和暂停计时器相关推荐

  1. android如何暂停倒计时,Android计时器和倒计时的实现(含开始,暂停,和复位)...

    倒计时和计时在比赛中经常用到,由于涉及到子线程和主线程的通信问题,同学们在制作的时候总有点畏手畏脚的,今天会有两个神器出现,帮助大家攻克这两道难关! 神器一:chronometer --andrdoi ...

  2. android 播放mp3 倒计时,Android实战 - 音心播放器 (MusicActivity - 倒计时 ,进度条实现)...

    1.背景 还是音乐播放界面,实现倒计时和进度条功能,基本实现过程: 当打开MusicActivity 的时候,MusicService会发送广播给MusicActivity ,后开始当前播放的时间进度 ...

  3. Android图片上倒计时,Android自定义照相机倒计时拍照

    自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件: 两个TextView是用来显示提示信息和倒计时的秒数的 xmlns:tools="http://schemas ...

  4. android 酷炫倒计时,android 好用的倒计时

    android倒计时 是在原有TextView的基础上进行二次开发的自定义View 效果图 这个主要是结合了android的CountDownTimer,这个一般就是发送短信的倒计时. 包含功能 倒计 ...

  5. android 天时分倒计时,Android倒计时(分钟)

    本文通过CountDownTimer来实现倒计时的功能,先上效果图 效果图.gif 1.核心方法就是通过onTick方法来获取时间的改变 public void onTick(long millisU ...

  6. android实现首页倒计时,Android倒计时 Android仿京东倒计时 android电商app源码倒计时源码...

    一个简单的计时器 提供了一些方法 设置margin public void setLinearLayoutMargin(int left, int top, int right, int bottom ...

  7. android带方框倒计时,简单漂亮的彩色方块计时器和倒计时js代码

    js代码 /** * time.day 和 time.dayZero 区别 * time.day 如果是1天 返回 1 * time.dayZero 如果是1天 则返回 01 * 除了day拥有Zer ...

  8. android实现首页倒计时,android 利用CountDownTimer实现时分秒倒计时效果

    利用name或id属性设置页面跳转的锚点 理论准备         网页中的链接按照链接路径的不同,可以分为3种类型,分别是内部类型.锚点链接和外部链接:         按照使用对象的不同,网页中的 ...

  9. android 倒计时 源码,Android倒计时 Android仿京东倒计时 android电商app源码倒计时源码...

    countdown 一个简单的计时器 提供了一些方法 设置margin public void setLinearLayoutMargin(int left, int top, int right, ...

最新文章

  1. UNITY调用安桌方法出现 JNI: Init'd AndroidJavaClass with null ptr!
  2. python123江湖秘籍_江湖秘籍,python基本数据类型之列表、元组、字典
  3. SnipperImages(Silverlight DEMO)控件设计之--Slider和ColorSlider
  4. 如何熟悉一个开源项目
  5. Vue之前端页面使用json编辑框
  6. java并发核心知识体系精讲_Java 面试突击之 Java 并发知识基础 amp; 进阶考点全解析
  7. CVPR 2020 | 北大Futurewei提出 GraphTER:无监督图变换共变表征学习
  8. SpringBoot防XSS攻击
  9. nodejs-express
  10. 周三直播预告丨openGauss和MogDB的优化分享
  11. python与java前景-数据告诉你,Python、Java、C+哪个更有发展前途?
  12. HDU-1394 Minimum Inversion Number 线段树+逆序对
  13. Android微信分享功能实例+demo
  14. elixir元编程的quote与unquote
  15. win10开始菜单打不开,使用startmenu.diagcab进行修复
  16. 【MySQL】Schema与数据类型优化
  17. cmd命令让计算机崩溃,十大危险的cmd指令,谨慎使用
  18. c++ unescape
  19. 教你如何简便下载网站上的视频
  20. 并行处理及分布式系统 第二章 并行硬件和并行软件

热门文章

  1. 创业者ALL IN的身段
  2. 分歧还是共存?详解Android内核安全
  3. CCAI 2017 | 谭铁牛院士现场致辞:人工智能新热潮下要保持清醒头脑_设定科学的目标...
  4. 新瑞鹏冲刺上市:持续亏损,旗下宠物医院屡被罚,彭永鹤为董事长
  5. 《Android编程兵书》PDF版电子书下载
  6. LINUX下载安装nvm
  7. 解决校园Dr客户端端口占用问题(2)
  8. 图片怎么转为html5,将图片转化为矢量并canvas化的容易工具(基于Node.js + HTML5 canvas)...
  9. 自适应滤波器之 LMS 算法
  10. golang syscall 系统调用认知