We are developing an application that shows the same text at fixed intervals on the Android emulator screen. I'm writing a Handler class now.
handler = new Handler();
Runnable r = new Runnable() {
public void run() {
tv.append("Hello World");
}
};
handler.postDelayed(r, 1000);
The problem is that when I launch the app, the text is displayed only once. Why is that?
android multithreading
Fix it like below.
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
tv.append("Hello World");
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
Or you can just use the thread.
Thread thread = new Thread() {
@Override
public void run() {
try {
while(true) {
sleep(1000);
handler.post(this);
}
} } catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
Since the handler does not continue to operate, you must call the method so that threads continue to be called inside the handler.
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
914 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.