我已经开发了一个倒数计时器,但不确定单击计时器的文本视图时如何暂停和恢复计时器。单击开始,然后再次单击以暂停并继续,再次单击计时器的文本视图。
这是我的代码:
Timer = (TextView) this.findViewById(R.id.time); //TIMER
Timer.setOnClickListener(TimerClickListener);
counter = new MyCount(600000, 1000);
}//end of create
private OnClickListener TimerClickListener = new OnClickListener() {
public void onClick(View v) {
updateTimeTask();
}
private void updateTimeTask() {
if (decision == 0) {
counter.start();
decision = 1;
} else if (decision == 2) {
counter.onResume1();
decision = 1;
} else {
counter.onPause1();
decision = 2;
}//end if
}
;
};
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}//MyCount
public void onResume1() {
onResume();
}
public void onPause1() {
onPause();
}
public void onFinish() {
Timer.setText("00:00");
p1++;
if (p1 <= 4) {
TextView PScore = (TextView) findViewById(R.id.pscore);
PScore.setText(p1 + "");
}//end if
}//finish
public void onTick(long millisUntilFinished) {
Integer milisec = new Integer(new Double(millisUntilFinished).intValue());
Integer cd_secs = milisec / 1000;
Integer minutes = (cd_secs % 3600) / 60;
Integer seconds = (cd_secs % 3600) % 60;
Timer.setText(String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds));
///long timeLeft = millisUntilFinished / 1000;
/}//on tick
}//class MyCount
protected void onResume() {
super.onResume();
//handler.removeCallbacks(updateTimeTask);
//handler.postDelayed(updateTimeTask, 1000);
}//onResume
@Override
protected void onPause() {
super.onPause();
//do stuff
}//onPause
/*
* Copyright (C) 2010 Andrew Gainer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Adapted from Android's CountDownTimer class
package com.cycleindex.multitimer;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
/**
* Schedule a countdown until a time in the future, with
* regular notifications on intervals along the way.
*
* 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 class CountDownTimerWithPause {
/**
* Millis since boot when alarm should stop.
*/
private long mStopTimeInFuture;
/**
* Real time remaining until timer completes
*/
private long mMillisInFuture;
/**
* Total time on timer at start
*/
private final long mTotalCountdown;
/**
* The interval in millis that the user receives callbacks
*/
private final long mCountdownInterval;
/**
* The time remaining on the timer when it was paused, if it is currently paused; 0 otherwise.
*/
private long mPauseTimeRemaining;
/**
* True if timer was started running, false if not.
*/
private boolean mRunAtStart;
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start} until the countdown is done and {@link #onFinish()}
* is called
* @param countDownInterval The interval in millis at which to execute
* {@link #onTick(millisUntilFinished)} callbacks
* @param runAtStart True if timer should start running, false if not
*/
public CountDownTimerWithPause(long millisOnTimer, long countDownInterval, boolean runAtStart) {
mMillisInFuture = millisOnTimer;
mTotalCountdown = mMillisInFuture;
mCountdownInterval = countDownInterval;
mRunAtStart = runAtStart;
}
/**
* Cancel the countdown and clears all remaining messages
*/
public final void cancel() {
mHandler.removeMessages(MSG);
}
/**
* Create the timer object.
*/
public synchronized final CountDownTimerWithPause create() {
if (mMillisInFuture <= 0) {
onFinish();
} else {
mPauseTimeRemaining = mMillisInFuture;
}
if (mRunAtStart) {
resume();
}
return this;
}
/**
* Pauses the counter.
*/
public void pause () {
if (isRunning()) {
mPauseTimeRemaining = timeLeft();
cancel();
}
}
/**
* Resumes the counter.
*/
public void resume () {
if (isPaused()) {
mMillisInFuture = mPauseTimeRemaining;
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
mPauseTimeRemaining = 0;
}
}
/**
* Tests whether the timer is paused.
* @return true if the timer is currently paused, false otherwise.
*/
public boolean isPaused () {
return (mPauseTimeRemaining > 0);
}
/**
* Tests whether the timer is running. (Performs logical negation on {@link #isPaused()})
* @return true if the timer is currently running, false otherwise.
*/
public boolean isRunning() {
return (! isPaused());
}
/**
* Returns the number of milliseconds remaining until the timer is finished
* @return number of milliseconds remaining until the timer is finished
*/
public long timeLeft() {
long millisUntilFinished;
if (isPaused()) {
millisUntilFinished = mPauseTimeRemaining;
} else {
millisUntilFinished = mStopTimeInFuture - SystemClock.elapsedRealtime();
if (millisUntilFinished < 0) millisUntilFinished = 0;
}
return millisUntilFinished;
}
/**
* Returns the number of milliseconds in total that the timer was set to run
* @return number of milliseconds timer was set to run
*/
public long totalCountdown() {
return mTotalCountdown;
}
/**
* Returns the number of milliseconds that have elapsed on the timer.
* @return the number of milliseconds that have elapsed on the timer.
*/
public long timePassed() {
return mTotalCountdown - timeLeft();
}
/**
* Returns true if the timer has been started, false otherwise.
* @return true if the timer has been started, false otherwise.
*/
public boolean hasBeenStarted() {
return (mPauseTimeRemaining <= mMillisInFuture);
}
/**
* Callback fired on regular interval
* @param millisUntilFinished The amount of time until finished
*/
public abstract void onTick(long millisUntilFinished);
/**
* Callback fired when the time is up.
*/
public abstract void onFinish();
private static final int MSG = 1;
// handles counting down
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountDownTimerWithPause.this) {
long millisLeft = timeLeft();
if (millisLeft <= 0) {
cancel();
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
long delay = mCountdownInterval - (SystemClock.elapsedRealtime() - lastTickStart);
// special case: user's onTick took more than mCountdownInterval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}
资料来源:本要点。
问题内容: 我在viewDidAppear中有一个图像,并用以下代码对其进行了动画处理: 我想在点击时暂停动画,如果再次点击它则继续播放动画。 问题答案: 暂停和恢复动画的2个功能,我从这里开始转换为Swift。 我有一个按钮可以暂停或恢复在中初始化的动画:
问题内容: 我有一个基本的Swing UI,带有一个标记为“播放”的按钮。按下按钮后,标签变为“暂停”。现在,当按下按钮时,它变为“继续”。 在“播放”中,我将实例化并执行一个SwingWorker。我想要的是能够暂停该线程(不要取消该线程),并根据上述按钮按下来恢复它。但是,我不想在doInBackground()中求助于Thread.sleep()。这似乎有点骇人听闻。有什么方法可以阻止运行d
我要做的是暂停< code>KafkaConsumer,如果在使用消息的过程中出现错误。 这是我写的 然后我写了一个REST服务来恢复消费者 现在,我有两个问题。第一个问题:当我打电话给消费者时。来自<code>@KafkaListener</code>注释方法的pause()会发生什么?消费者立即暂停,或者我可以接收到同一主题分区的其他偏移量上的其他消息。例如,我有偏移量为3的“message1
问题内容: 我正在开发游戏,我想创建一个暂停菜单。这是我的代码: 但 仍在运行… 我想在玩家单击暂停菜单时暂停计时器,并在玩家返回游戏时继续运行计时器,但是我如何暂停?请帮帮我。 问题答案: 您需要使其无效并重新创建。然后,如果您使用相同的按钮暂停和恢复计时器,则可以使用bool来跟踪状态:
我正在构建具有恢复/暂停功能的m4a录制的Android应用程序。可能吗?如果是,请告诉我解决方案。 谢谢
怎么停止这个计时器,知道吗? 我想重置计时器在每个查询,但它继续。每个新的查询都会添加新的计时器。这个怎么解决?