Recently I've been doing Android development with the book Beginning Android Games. It contains the following code:
public class KeyboardHandler implements OnKeyListener {
boolean[] pressedKeys = new boolean[128];
Pool<KeyEvent> keyEventPool;
List<KeyEvent> keyEventsBuffer = new ArrayList<KeyEvent>();
List<KeyEvent> keyEvents = new ArrayList<KeyEvent>();
public KeyboardHandler(View view) {
PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {
public KeyEvent createObject() {
return new KeyEvent();
}
};
keyEventPool = new Pool<KeyEvent>(factory, 100);
view.setOnKeyListener(this);
view.setFocusableInTouchMode(true);
view.requestFocus();
}
public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
if (event.getAction() == android.view.KeyEvent.ACTION_MULTIPLE)
return false;
synchronized (this) {
KeyEvent keyEvent = keyEventPool.newObject();
keyEvent.keyCode = keyCode;
keyEvent.keyChar = (char) event.getUnicodeChar();
if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
keyEvent.type = KeyEvent.KEY_DOWN;
if(keyCode > 0 && keyCode < 127)
pressedKeys[keyCode] = true;
}
if (event.getAction() == android.view.KeyEvent.ACTION_UP) {
keyEvent.type = KeyEvent.KEY_UP;
if(keyCode > 0 && keyCode < 127)
pressedKeys[keyCode] = false;
}
keyEventsBuffer.add(keyEvent);
}
return false;
}
Once again I was a bit confused on why synchronized
was necessary, and the book states:
"Remember
that the events are received on the UI thread and read on the main loop thread, so we
have to make sure none of our members are accessed in parallel. "
First of all, I thought the UI thread was the same as the main thread(or main loop thread, is there a difference?) Say the UI thread is executing this code, therefore also receiving the events(since this class implements OnClickListener). Say the main loop, the while(!userquit)
loop, runs on a seperate thread. How are the events read by the main loop thread? Also, shouldn't the main loop run on the UI thread? Is my notion about Event Handling correct? When a keyEvent occurs, the thread executing KeyboardHandler has the code onKey() executed. How are events received on the UI thread, but read on the Main Loop thread? Shouldn't events be recieved and handled on the same thread?