I have an app in which it is essential that I execute some code when the user presses the centre button on an android device. I already process the user pressing the right hand button (the back button) via onKeyDown(int keyCode, KeyEvent event) / if (keyCode == KeyEvent.KEYCODE_BACK) so I had assumed that pressing the centre button would be processed in a similar manner. But upon further investigation, it appears that onKeyDown is not called. So my question is; how to I intercept a centre-button press?
P.S. Much to my embarrassment, I’m not even sure what the centre button is called!
You can’t intercept that key (if you are talking about the home-button)
It is an OS function, which takes the user to his launcher on short-press, and to the multitask-menu on long-press.
If is also a Core-app Quality Guideline.
What you could detect is your application being sent to the background using
onStop().Before Android 3.0, activities could be killed directly from
onPause()without hittingonStop(). So if you are targeting pre-honeycomb devices as well, you might need to look at theonPause()event as well.