【问题标题】:Capture the countdown timer in different activities捕捉不同活动中的倒计时
【发布时间】:2012-02-28 16:31:26
【问题描述】:

我正在设计一个倒数计时器,它可以完美运行,但前提是我留在这个Activity 中。因为,如果倒计时开始并且我例如返回并开始其他Activity,则countdonws 会跟随倒计时。从逻辑上讲,如果我再次开始倒计时计时器所在的活动,似乎没有什么在倒计时,但是在免费倒计时,我知道,因为在计时器开始时,我会抛出一个继续通知以显示它通知标签。

代码如下,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);
        }
    }   
}   

}

所以我要做的是知道何时返回Activity 并开始另一个,以显示时间在后台倒计时以在例如文本视图中显示它。

或者,如果我可以完美地从通知选项卡中获得倒计时的时间。 谢谢!

【问题讨论】:

    标签: android countdowntimer


    【解决方案1】:

    将您的 MyCounter 实例移出 Activity。当 Activity 重新启动时,正在重新创建实例,因此旧的实例消失了。有几种方法可以做到这一点,但您需要让 MyCounter 实例位于 Activity 可以访问它但不对其生命周期负责的地方。我使用自己编写的基本 ServiceLocator 实现,但您可以轻松创建自定义 Application 类并将计时器(或创建和控制计时器的控制器类)保留在其中。

    【讨论】:

    • 我迷路了!!可以获取我在通知栏中显示的时间吗?因为每一秒都有倒计时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多