I want to pause chronometer and after I click the button I want to continue chromoneter to count... I search but couldn't a function related this.. how can do it?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
The two other answers are identical, and work very well on the Chronometer's display, but they have one flaw:
timeWhenStopped
, as well as the value returned bygetCurrentTime()
, is negative.Here is my suggestion, base on these two answers:
You've got to understand
SystemClock.elapsedRealtime()
as an indicator for "now". So when we (re)start the chronometer, we shall set the baseN
seconds in the past,N
being the timer's current value (0 in case of first start). Similarly, when stopping the chronometer, the time displayed by the chronometer is the time elapsed between the previously set base (getBase()
) and now (SystemClock.elapsedRealtime()
), hence the subtraction.I made a
PauseableChronometer
class for this.You are gonna need a variable that keeps track on the time that has passed since the Chronometer was started:
Update the value of the variable when you stop the chronometer like this:
We will also use this variable to adjust the chronometer before starting it:
And finally if you have a way to reset your chronometer then you should remember to also reset the timeWhenStopped variable. Something like this: