调用onPause时,倒计时器仍会继续

时间:2016-01-23 11:02:47

标签: java android countdowntimer onpause

我正在创建一个简单的应用程序,我开始倒数计时器,例如25到0,当计时器到期时,它显示一个模板。但是当我的手机被锁定时,计时器将暂停,当我解锁时,它将恢复。我能为继续计时器做什么?

public class MyActivity extends Activity {
TextView tvCountDown;
MyCount counter;
long countDownInterval = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_auth);
    tvCountDown = (TextView) findViewById(R.id.tvCountDown);
    counter = new MyCount(6000, countDownInterval);
        counter.start();
}
public class MyCount extends CountDownTimer 
{

    public MyCount(long millisInFuture, long countDownInterval) 
    {
        super(millisInFuture, countDownInterval);
    }
    public void onTick (long millisUntilFinished) 
    {
        Log.v("test","onTick Seconds: "+millisUntilFinished+" : "+(millisUntilFinished / 1000));
        tvCountDown.setText ( formatTime(millisUntilFinished));
        System.out.print("");
    }
    public void onFinish() {
        tvCountDown.setText("done!");
    }
    public String formatTime(long millis) 
    {
        String output = "00:00:00";
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;

        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 60;

        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);
        String hoursD = String.valueOf(hours); 

        if (seconds < 10)
            secondsD = "0" + seconds;
        if (minutes < 10)
            minutesD = "0" + minutes;
        if (hours < 10)
            hoursD = "0" + hours;

        output = hoursD + " : " + minutesD + " : " + secondsD;
        return output;
    }
}

1 个答案:

答案 0 :(得分:0)

当你的活动/片段破坏时,只需调用function add_specific_menu_location_atts( $atts, $item, $args ) { if( $args->theme_location == 'directentries' ) { foreach($item as $key => $value) { if($key == 'title') { $catIcon = setCatIcon($value); } } foreach($args as $key => $value) { if($key == 'link_before') { $args->$key = '<span class="glyphicon glyphicon-'.$catIcon.'"></span><br>'; } } // add the desired attributes: $atts['class'] = 'btn btn-primary btn-lg'; } return $atts; } add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 ); 方法即可。

如果您想暂停/恢复计时器,请转到this answer;