0

I have made a countdown timer where I set the time that I want and it counts down this runs in a loop increasing each round once timer hits 0. I have manage to get this working but once I open other apps that force the program to close due and I return to the timer it has lost it current information require me to start from the start.

How would I go about preventing the app from closing and keeping it to continuously run, until the user decides to close the program.

Also I a new to Android coding first program I have made for it. So very new to the whole system.

4

1 回答 1

0

请看一下 timers 方法:

 Timer.scheduleAtFixedRate(yourtimertask, yourdelay, yourinterval);  

每次间隔时间过去时,该方法都会执行 TimerTask 中的代码。

这是 Activity 中的 Timer 的简单示例:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);

    MyTimerTask yourTask = new MyTimerTask();
    Timer t = new Timer();

    t.scheduleAtFixedRate(yourTask, 0, 10000);  // 10 sec interval      
}

class MyTimerTask extends TimerTask {
      public void run() {
          // do whatever you want in here
      }
}

我创建了自己的 TimerTask,我可以在它的 run 方法中做一些事情。在 onCreate() 方法中,我启动计时器并每 10 秒执行一次 Tasks run() 方法中的代码。

此外,您还应该看看 Android AlarmManagerPendingIntents

在这个不错的教程中阅读更多相关信息:

http://learnandroideasily.blogspot.co.at/2013/05/android-alarm-manager_31.html

于 2013-08-23T13:56:11.157 回答