How to simulate home, back, menu, search, task and volume buttons?

Meroelyth picture Meroelyth · Dec 14, 2012 · Viewed 12.5k times · Source

I want to simulate all physical buttons of our Android devices.

So is there a way to simulate:

  • BACK BUTTON
  • HOME BUTTON
  • MENU BUTTON
  • SEARCH BUTTON
  • TASK BUTTON
  • VOLUME (+ AND -) BUTTONS

Answer

Aleksander Gralak picture Aleksander Gralak · Dec 14, 2012

Create a KeyEvent and publish it.

KeyEvent kdown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
Activity.dispatchKeyEvent(kdown);
KeyEvent kup = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
Activity.dispatchKeyEvent(kup);