每 15 秒循环一次,它开始正常,但最近它每秒运行一次
A loop action every 15 seconds it starts ok but lately it runs every second
我的 MainActivity.java 中有一个循环:
public class MainActivity <...> {
Handler handler = new Handler();
Runnable runnable;
int delay = 15000;
}
@Override
public void onResume() {
//super.onResume();
handler.postDelayed(runnable = new Runnable() {
public void run() {
handler.postDelayed(runnable, delay);
Log.d("myTag", "This is my message");
}
}, delay);
super.onResume();
}
如前所述,在开始时,每 15 秒执行一次 运行s(不准确,有时 13 秒,有时 16 秒,等等,但它保持 运行ning)在某个时刻每秒开始 运行:
Logcat 最后条目:
2021-12-08 18:06:35.329 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:38.135 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:39.318 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:39.667 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:48.967 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:50.330 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:53.138 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:54.320 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:54.669 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:07:03.968 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:07:05.332 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:07:08.140 25533-25533/com.app.xxxx D/myTag: This is my message
有什么想法吗?为什么会这样?
正如@JayC667 所说,每次 onResume()
被调用时你 post 这个。因此,您将拥有此 运行 的多个副本。为防止这种情况,您可以 post 在 onCreate()
中 Runnable
或确保删除 onPause()
中的任何 posted Runnable
。 =15=]
我的 MainActivity.java 中有一个循环:
public class MainActivity <...> {
Handler handler = new Handler();
Runnable runnable;
int delay = 15000;
}
@Override
public void onResume() {
//super.onResume();
handler.postDelayed(runnable = new Runnable() {
public void run() {
handler.postDelayed(runnable, delay);
Log.d("myTag", "This is my message");
}
}, delay);
super.onResume();
}
如前所述,在开始时,每 15 秒执行一次 运行s(不准确,有时 13 秒,有时 16 秒,等等,但它保持 运行ning)在某个时刻每秒开始 运行:
Logcat 最后条目:
2021-12-08 18:06:35.329 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:38.135 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:39.318 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:39.667 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:48.967 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:50.330 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:53.138 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:54.320 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:06:54.669 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:07:03.968 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:07:05.332 25533-25533/com.app.xxxx D/myTag: This is my message
2021-12-08 18:07:08.140 25533-25533/com.app.xxxx D/myTag: This is my message
有什么想法吗?为什么会这样?
正如@JayC667 所说,每次 onResume()
被调用时你 post 这个。因此,您将拥有此 运行 的多个副本。为防止这种情况,您可以 post 在 onCreate()
中 Runnable
或确保删除 onPause()
中的任何 posted Runnable
。 =15=]