protected void onResume() { super.onResume(); if (null == timer) { timer = new Timer(); startTiming(); } }
时间: 2024-02-15 07:27:40 浏览: 28
这是一个 Android 应用程序中的 onResume() 方法,它会在 Activity 进入前台时被调用。在这个方法中,首先调用了父类的 onResume() 方法,然后判断计时器对象 timer 是否为 null,如果是,则创建一个新的 Timer 对象,并调用 startTiming() 方法开始计时。这段代码的作用是确保计时器在 Activity 进入前台时始终处于运行状态。
相关问题
timer = new CountDownTimer(Long.MAX_VALUE, 1000),当跳转页面后timer暂停计时,当回到该页面时继续计时
要实现这个功能,你可以在页面跳转的时候记录下当前的计时器时间,然后在回到页面的时候恢复计时器,并将计时器时间设置为之前记录的时间。
具体的实现方法如下:
1. 在页面跳转之前,记录下当前计时器的时间,可以使用 onSaveInstanceState 方法将时间保存到 Bundle 中。
2. 在回到页面的时候,先从 Bundle 中恢复之前保存的时间,然后重新创建一个 CountDownTimer 对象,并将时间设置为之前保存的时间。
代码示例:
```
public class MainActivity extends AppCompatActivity {
private static final String KEY_TIMER_TIME = "timer_time";
private CountDownTimer timer;
private long timerTime = Long.MAX_VALUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
timerTime = savedInstanceState.getLong(KEY_TIMER_TIME);
}
timer = new CountDownTimer(timerTime, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// 计时器每秒回调一次
}
@Override
public void onFinish() {
// 计时器完成后回调
}
};
timer.start();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(KEY_TIMER_TIME, timer != null ? timer.getTime() : Long.MAX_VALUE);
}
@Override
protected void onResume() {
super.onResume();
if (timer != null) {
timer.cancel();
timer = new CountDownTimer(timerTime, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// 计时器每秒回调一次
}
@Override
public void onFinish() {
// 计时器完成后回调
}
};
timer.start();
}
}
}
```
在 onSaveInstanceState 方法中,我们将计时器的时间保存到 Bundle 中。在 onResume 方法中,我们从 Bundle 中恢复时间,并重新创建一个 CountDownTimer 对象,并将时间设置为之前保存的时间。这样就可以保证在页面跳转后,计时器暂停计时,当回到该页面时继续计时。
Android使用Timer计时输出TextView格式时间
可以使用以下代码来实现:
首先,在你的布局文件中添加一个TextView组件,用于显示计时器的时间:
```xml
<TextView
android:id="@+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="00:00:00" />
```
然后,在你的Activity或Fragment中,声明一个Timer对象和一个TimerTask对象,并在onCreate方法中初始化它们:
```java
private Timer timer;
private TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = new Timer();
timerTask = new TimerTask() {
int seconds = 0;
int minutes = 0;
int hours = 0;
@Override
public void run() {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
hours++;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView timerTextView = findViewById(R.id.timerTextView);
timerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
});
}
};
}
```
在上面的代码中,我们定义了一个计时器,每一秒钟更新一次TextView的文本内容。我们还使用runOnUiThread方法来确保更新UI的操作在UI线程上执行。
现在,我们需要在Activity或Fragment的生命周期方法中启动和停止计时器。在onResume方法中启动计时器,在onPause方法中停止计时器:
```java
@Override
protected void onResume() {
super.onResume();
timer.scheduleAtFixedRate(timerTask, 0, 1000);
}
@Override
protected void onPause() {
super.onPause();
timer.cancel();
}
```
在上面的代码中,我们使用scheduleAtFixedRate方法来启动计时器,并在onPause方法中取消计时器。
现在,你就可以在你的Android应用程序中使用Timer计时器来显示格式化的时间了!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)