1

Dows anybody know how I can simulate pressing the back key by software? I've seen some applications which simulate this key and a few other keys like home and search. But because theese weren't open source, I tried to build my own application which can manage theese tasks. To go back to home for example is very easy because I had to send just one intent. So... what can I do to simulate the back key? :)

UPDATE:

I've downloaded an open source application which has the same functions from http://git.hoopajoo.net/

There it's done like that:

Process process = Runtime.getRuntime().exec("su");
OutputStream outputStream = process.getOutputStream();
String cmd = "keycode " + KeyEvent.KEYCODE_BACK;
outputStream.write((cmd + "\n").getBytes("ASCII"));

Where does the "keycode" command come from? I could not found any so called binary in my Android rootfs?!?!

2
  • 1
    Why do you want to simulate the Back key? What is the purpose?
    – gnclmorais
    Apr 29, 2011 at 13:47
  • This application will be neccessary because I am running Android not on a mobile but on an embedded device that does not have any hardware keys.
    – Charly
    Apr 29, 2011 at 13:54

4 Answers 4

12

Aleadam method don't work on my android 4.1.2. I don't know why, but this workaround works:

public void dispachBackKey() {
    dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
    dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}
1
  • Thanks... I was searching for an entire afternoon calling ACTION_DOWN only was not working... Calling both events (together making up the click) works perfectly. :)
    – Scrat
    May 24, 2013 at 12:32
5

To simulate a key press, you can dispatch a KeyEvent:

myView.getRootView().dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

An alternative solution for this situation is to have an ongoing notification (see http://developer.android.com/reference/android/app/NotificationManager.html ) that will stop the service when clicked.

4
  • Hmm nothing happens when I put this into my listener. backButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "### onClick (backButton)"); v.getRootView().dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); } }); I get the log message but nothing more happens :(
    – Charly
    Apr 29, 2011 at 14:14
  • Does your Activity handle the keyevent? But if you are going to have a button, just use that clickevent to call the getContext().stopService (Intent service)
    – Aleadam
    Apr 29, 2011 at 14:15
  • The code above is to be handled by the Activity, not the Service. getRootView() returns the root View of that same activity. See also an alternative above.
    – Aleadam
    Apr 29, 2011 at 14:21
  • @Aleadam Is it possible to use your code from a floating view (which exist in a Service) to simulate the back key on the foreground activity? Aug 16, 2016 at 16:13
2

I had exactly the same problem as you. Turns out the syntax of commands in runCommand used by SoftKeys is different from the shell.

In shell to emulate key event, use input keyevent. Looking up the documentation, key code for back button () is 4. So do this:

input keyevent 4

http://forum.xda-developers.com/showthread.php?t=971768

http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BACK

0

You should call the onKeyDown() function. THe keycode isn't hard to get, but you might need to do something to give a sane event.

2
  • You definitely shouldn't call it, override it instead. Apr 29, 2011 at 14:34
  • 2
    I disagree: He wants to SIMULATE the back button, not OVERRIDE it. To do the latter, you want to write your own function, but to simulate the behaviour you should make an onclick that calls it.
    – Nanne
    Apr 29, 2011 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.