Android 捕获不同活动中的倒计时计时器
我正在设计一个倒计时计时器,它工作得很好,但前提是我一直在这个Android 捕获不同活动中的倒计时计时器,android,countdowntimer,Android,Countdowntimer,我正在设计一个倒计时计时器,它工作得很好,但前提是我一直在这个活动中。因为,如果倒计时开始,例如,我返回并启动其他活动,则倒计时之后是倒计时。从逻辑上讲,如果我再次启动倒计时计时器所在的活动,看起来好像什么都没有倒计时,但在背景中倒计时,我知道,因为在计时器的点击位置,我抛出一个继续通知,在通知选项卡上显示它 代码如下所示,sendnotification方法是一种在时间结束时抛出警报的方法,sendnotificationTiempo每秒向选项卡发送一次通知,并显示在顶部 public sta
活动中。因为,如果倒计时开始,例如,我返回并启动其他活动
,则倒计时之后是倒计时。从逻辑上讲,如果我再次启动倒计时计时器所在的活动,看起来好像什么都没有倒计时,但在背景中倒计时,我知道,因为在计时器的点击位置,我抛出一个继续通知,在通知选项卡上显示它
代码如下所示,sendnotification方法是一种在时间结束时抛出警报的方法,sendnotificationTiempo
每秒向选项卡发送一次通知,并显示在顶部
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);
}
}
}
}
所以我要做的是,知道我什么时候返回活动,然后启动另一个活动,在后台显示倒计时的时间,例如在文本视图中显示
或者,如果我能从通知选项卡中获得倒数计时的时间。
谢谢 将MyCounter实例移出活动。当活动重新启动时,将重新创建实例,以便旧实例消失。有几种方法可以做到这一点,但您需要让您的MyCounter实例位于某个活动可以访问它但不负责其生命周期的地方。我使用我编写的基本ServiceLocator实现,但您可以轻松创建自定义应用程序类并将计时器(或创建和控制计时器的控制器类)保留在那里。buff我迷路了!!可以得到我在通知栏中显示的时间吗?因为每秒钟都有倒计时