I have a loop in my 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();
}
As said, at the beginning, the action runs every 15 seconds (not exact, sometimes 13, sometimes 16, etc., but as it keeps running) at some point starts to run every second:
Logcat of last entries:
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
Any ideas? Why is this happening?
CodePudding user response:
As @JayC667 says, you post this every time onResume()
is called. So you will have multiple copies of this running. To prevent that, you can either post your Runnable
in onCreate()
or make sure that you remove any posted Runnable
in onPause()
.
CodePudding user response:
If it run every second try to this
Int count =0;
If (count == 15){
Count =0;
Your code here
} Else {count }