Touch Event Flooding
From Android Game Development Wiki
Contents |
[edit] The Problem
Touch event flooding is common on first generation devices running Android version 1.5/1.6. The touch panel driver sends out events too frequently which get subsequently dispatched to the Java side UI thread at a high frequency. As most games use a secondary thread for the game logic and/or rendering the constant scheduling of the UI thread takes away precious CPU cycles from those vital threads. This results in lower frame rates for GPU intensive games but also has an impact on moderatly complex games.
This is connected the high CPU usage problem that is still there in Android versions greater than 1.6. The driver actually generates a lot more events that arrive at the Java end, so even in 1.5 some event dropping seems to be performed just not enough. The main bulk of CPU usage is probably happening in the kernel itself due to the bad driver behaviour. Note that this is only speculation as Google engineers are light on details.
[edit] The Solution
Android devices running version 2.0 or higher do not suffer from the flooding anymore as this issue was fixed on a system level. On pre 2.0 devices, especially first generation devices one usually resorts to sleeping in the OnTouchListener.onTouch() method between 10 and 40ms. This of course introduces a certain degree of input lag so balancing the sleep time is mandatory on a case by case basis.
Note: this will not solve the CPU usage problem. Sleeping can have a positive impact on that issue as well but the effect is much less pronounced.
[edit] Sample Code
A simple demonstration of touch even flooding can be found in the Android Game Development Wiki samples project. The corresponding source file is located at http://code.google.com/p/agd-wiki-samples/source/browse/trunk/src/com/badlogic/agdwikisamples/TouchFloodSample.java. The sample allows you to observe the number of motion events dispatched to the UI thread while touching the screen. On first generation devices like the Hero or G1 running Android 1.5/1.6 a lot of events are send each second taking up processing time on the UI thread that other threads might need. On newer devices like the Droid running Android version 2.0 or higher the flooding is gone due to the bug being fixed on a system level. You can toggle sleeping in the OnTouchListener.onTouch() method by pressing the corresponding button at the top of the activity.
[edit] External Links
First discussion of the problem on the Android developer group
Another discussion on the Android developer group showing that sleeping alone does not fix the issue