I'm trying to implement a simple game with Android sdk. This game uses a custom view as the game area. I tried to use this code snippet to add a simple clock to the game. Basically it launches a thread which calls invalidate
on the custom game view with a fixed frequency:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
class GameClock extends Thread {
CanvasView cv;
public GameClock(Activity ctx) {
cv = ctx.findViewById(R.id.MainGame);
}
public void run() {
while (! cv.initialized) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
}
// TODO: fix this
while (true) {
cv.invalidate();
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
}
}
}
new GameClock(this).run();
}
But when I launch the app, it just hangs and doesn't display anything. Why does this happen? How can I fix it to work as expected?