即使我的活动被破坏,如何保持Countdowntimer运行

时间:2018-10-20 18:15:01

标签: java android service android-lifecycle countdowntimer

所以我知道我需要使用服务,所以我做了一个叫做 “公共类BroadcastService扩展了Service”,但是我不确定如何实现这些方法,因此有点迷失了。

这是我的MainActivity中的Countdowntimer,该活动在销毁活动时将被销毁:

library(tidyverse)

iris.summary <- iris %>% 
  gather(variable, value, -Species) %>% 
  group_by(variable, Species) %>% 
  summarize(
    min = min(value, na.rm = TRUE),
    max = max(value, na.rm = TRUE),
    median = median(value, na.rm = TRUE),
    MAD = mad(value, na.rm = TRUE),
    MAD_lowlim = median - (3 * MAD),
    MAD_highlim = median + (3 * MAD),
    Outliers_low = any(value < MAD_lowlim, na.rm = TRUE),
    Outliers_high = any(value > MAD_highlim, na.rm = TRUE)
  )

   variable     Species      min   max median   MAD MAD_lowlim MAD_highlim Outliers_low Outliers_high
   <chr>        <fct>      <dbl> <dbl>  <dbl> <dbl>      <dbl>       <dbl> <lgl>        <lgl>        
 1 Petal.Length setosa       1     1.9   1.5  0.148      1.06         1.94 TRUE         FALSE        
 2 Petal.Length versicolor   3     5.1   4.35 0.519      2.79         5.91 FALSE        FALSE        
 3 Petal.Length virginica    4.5   6.9   5.55 0.667      3.55         7.55 FALSE        FALSE        
 4 Petal.Width  setosa       0.1   0.6   0.2  0          0.2          0.2  TRUE         TRUE         
 5 Petal.Width  versicolor   1     1.8   1.3  0.222      0.633        1.97 FALSE        FALSE        
 6 Petal.Width  virginica    1.4   2.5   2    0.297      1.11         2.89 FALSE        FALSE        
 7 Sepal.Length setosa       4.3   5.8   5    0.297      4.11         5.89 FALSE        FALSE        
 8 Sepal.Length versicolor   4.9   7     5.9  0.519      4.34         7.46 FALSE        FALSE        
 9 Sepal.Length virginica    4.9   7.9   6.5  0.593      4.72         8.28 FALSE        FALSE        
10 Sepal.Width  setosa       2.3   4.4   3.4  0.371      2.29         4.51 FALSE        FALSE        
11 Sepal.Width  versicolor   2     3.4   2.8  0.297      1.91         3.69 FALSE        FALSE        
12 Sepal.Width  virginica    2.2   3.8   3    0.297      2.11         3.89 FALSE        FALSE   

我也已经在mainactivity中创建了startService,并在我的按钮中调用了它:

 private void startCountDown() { // is called via a button
    try {
        countdownElapsed = 0l;
        next_button.setEnabled(false);
        displayCountDown();
        mCountDownHandler.postDelayed(mCountDownRunnable, COUNT_DOWN_TICKS);
    } catch (Exception ex) {
        Log.e(TAG, ex.getMessage());
    }
}

private void displayCountDown() {
    long t = (COUNT_DOWN_FINISH - countdownElapsed) / 1000;
    String myTime = String.valueOf(t);
    timer.setText(myTime);
    if (countdownElapsed >= COUNT_DOWN_FINISH)
        timer.setText("Countdown");
}


private Runnable mCountDownRunnable = new Runnable() {
    @Override
    public void run() {
        countdownElapsed = countdownElapsed + COUNT_DOWN_TICKS;

        if (countdownElapsed >= COUNT_DOWN_FINISH) {
            releaseCountDownHandler();
            next_button.setEnabled(true);
        } else {

            mCountDownHandler.postDelayed(mCountDownRunnable, COUNT_DOWN_TICKS);
        }
        long secFull = countdownElapsed % 1000;

        if (secFull == 0) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    displayCountDown();
                }
            });
        }
    }

    private void releaseCountDownHandler() {
        try {
            if (mCountDownRunnable != null) {
                mCountDownHandler.removeCallbacks(mCountDownRunnable);
            }
        } catch (Exception ex) {
            Log.e(TAG, ex.getMessage());

        }
    }
};

我的广播服务类如下:

 public void startService() {
    Intent intent = new Intent(this, BroadcastService.class);
    startService(intent);
}

}

我想这是我想要做的事情所需要的所有方法,但是我不知道下一步在哪里以及用哪种方法写什么..我知道之前已经问过这个问题,但是我真的无法进一步进步和答案。

0 个答案:

没有答案