如何打破 CountDownTimer
How to break CountDownTimer
我是 android 的新手,我遇到了一个问题。
这是我的问题: 我是 运行 一个 CountDownTimer
30 秒,间隔 5 秒。当它运行时,我为每个间隔执行 activity。如果 activity returns 是肯定的结果,我会停止计时。如果计时器结束,我将宣布它失败。我尝试了很多方法,但我无法完成我需要的。
我不知道如何使用CountDownTimer
或Timer
?也许你这样做?
这是我的代码:
public void StartTimeOutTimer() {
countDown timer = new countDown(30000, 5000);
timer.start();
}
public class countDown extends CountDownTimer{
ProgressDialog pd;
public countDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
pd = ProgressDialog.show(Activity_MapMain.this, "",
"Loading new location...", true, false);
}
@Override
public void onTick(long millisUntilFinished) {
//Do an activity get new location
//if OK stop CountDownTimer ;
}
@Override
public void onFinish() {
pd.cancel();
}
}
尝试改用 Handler。类似的东西:
Handler handler;
boolean result;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
handler = new Handler();
handler.post(timerRunnable);
}
Runnable timerRunnable = new Runnable() {
int interval = 5000, period = 30000;
int count;
@Override
public void run() {
if (!result && count < period ) {
count += interval;
//call to the activity get new location with startActivityForResult()
handler.postDelayed(this, interval);
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
result = true;
}
}
编辑
如何开始 Activity 结果:
首先定义字段:
private final int CODE_NEW_LOCATION = 1;
在 Rubbable 里面放这样的东西:
startActivityForResult(new Intent(MainActivity.this, LocationActivity.class), CODE_NEW_LOCATION);
在 LocationActivity 中定义 return 值:
boolean result;
//define the result
setResult(Activity.RESULT_OK);
您可以在 CountDownTimer 对象中使用:this.cancel();
。
在您的 onTick 函数中尝试检查标志(布尔停止;)并禁用计数器。
示例:
boolean stop;
@Override
public void onTick(long millisUntilFinished) {
if (stop) {
stop = false;
cancel();
}
}
我是 android 的新手,我遇到了一个问题。
这是我的问题: 我是 运行 一个 CountDownTimer
30 秒,间隔 5 秒。当它运行时,我为每个间隔执行 activity。如果 activity returns 是肯定的结果,我会停止计时。如果计时器结束,我将宣布它失败。我尝试了很多方法,但我无法完成我需要的。
我不知道如何使用CountDownTimer
或Timer
?也许你这样做?
这是我的代码:
public void StartTimeOutTimer() {
countDown timer = new countDown(30000, 5000);
timer.start();
}
public class countDown extends CountDownTimer{
ProgressDialog pd;
public countDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
pd = ProgressDialog.show(Activity_MapMain.this, "",
"Loading new location...", true, false);
}
@Override
public void onTick(long millisUntilFinished) {
//Do an activity get new location
//if OK stop CountDownTimer ;
}
@Override
public void onFinish() {
pd.cancel();
}
}
尝试改用 Handler。类似的东西:
Handler handler;
boolean result;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
handler = new Handler();
handler.post(timerRunnable);
}
Runnable timerRunnable = new Runnable() {
int interval = 5000, period = 30000;
int count;
@Override
public void run() {
if (!result && count < period ) {
count += interval;
//call to the activity get new location with startActivityForResult()
handler.postDelayed(this, interval);
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
result = true;
}
}
编辑 如何开始 Activity 结果:
首先定义字段:
private final int CODE_NEW_LOCATION = 1;
在 Rubbable 里面放这样的东西:
startActivityForResult(new Intent(MainActivity.this, LocationActivity.class), CODE_NEW_LOCATION);
在 LocationActivity 中定义 return 值:
boolean result;
//define the result
setResult(Activity.RESULT_OK);
您可以在 CountDownTimer 对象中使用:this.cancel();
。
在您的 onTick 函数中尝试检查标志(布尔停止;)并禁用计数器。 示例:
boolean stop;
@Override
public void onTick(long millisUntilFinished) {
if (stop) {
stop = false;
cancel();
}
}