我正在设计一个倒数计时器,它完美的工作,但只有如果我留在这Activity
。因为,如果倒计时开始,我回头举例并开始其他Activity
,countdonws继续倒计时。从逻辑上讲,如果我重新开始倒数计时器的活动,看起来好像什么都没有倒计时,但在免费倒计时,我知道,因为在定时器的开始,我会发出一个继续通知,以显示它在通知选项卡。在不同的活动中捕捉倒数计时器
代码如下,sendnotification方法是在时间结束时抛出警报的方法,并且每秒向该选项卡发送通知以显示在顶部。
public static final int NOTIFICATION_ID = 33;
public static final int NOTIFICATION_ID_Tiempo = 34;
private int minCrono;
TextView text;
MyCounter timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StartCrono = (Button) findViewById(R.id.butt_start);
CancelCrono = (Button) findViewById(R.id.butt_cancel);
text=(TextView)findViewById(R.id.text_countTime1);
CancelCrono.setEnabled(false);
minCrono = mins.getCurrentItem();
StartCrono.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
minCrono=(60000*minCrono);
timer = new MyCounter(minCrono,1000);
timer.start();
}
});
CancelCrono.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timer.cancel();
text.setText("El tiempo se ha cancelado");
minutosCrono = mins.getCurrentItem();
if (mNotificationManager != null)
mNotificationManager.cancel(NOTIFICATION_ID_Tiempo);
}
});
}
倒计时类:
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
StartCrono.setEnabled(true);
CancelCrono.setEnabled(false);
mins.setEnabled(true);
minCrono = mins.getCurrentItem();
//
text.setText("Time Complete!");
sendnotification("Guau", "Time finished.");
}
@Override
public void onTick(long millisUntilFinished) {
long segRestantesTotal=(millisUntilFinished/1000);
long minRestantes= (segRestantesTotal/60);
long segRestantes= (segRestantesTotal%60);
if(segRestantes>=10){
text.setText(minRestantes+":"+segRestantes);
sendnotificationTiempo("Guau", minRestantes+":"+segRestantes);
}
else{
text.setText(minRestantes+":0"+segRestantes);
sendnotificationTiempo("Guau", minRestantes+":0"+segRestantes);
}
}
}
}
所以我会做的,就是知道我什么时候回去Activity
,我开始一个又一个,来显示时间是什么倒计时在后台显示,例如在textview中显示。
或者,如果我可以从通知选项卡倒计时完美的时间。 谢谢!
buff我失去了!我可以在通知栏中显示时间吗?因为每一秒都在倒计时 – Txispas 2012-02-28 18:25:37