I did some research and tried some code including the code below from Android postDelayed Handler Inside a For Loop?.
final Handler handler = new Handler();
int count = 0;
final Runnable runnable = new Runnable() {
public void run() {
// need to do tasks on the UI thread
Log.d(TAG, "runn test");
if (count++ < 5)
handler.postDelayed(this, 5000);
}
};
// trigger first time
handler.post(runnable);
But the count variable will show an error because it is accessed within the inner class. How can I solve the problem?