I am macking a custom navigation bar to Android 4.0.3.r1 and want to send key events like "Home" and "Back". My application is not a system therefore:
IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
ServiceManager.getService(Context.WINDOW_SERVICE));
mWindowManager.injectKeyEvent( ev, false );
It doesn't work, because I can not get android.permission.INJECT_EVENTS
from not system application. How can I do this?
Reviving old thread - You can perform Home and Back with the relatively new Accessibility API - Check out "performGlobalAction" here: http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
(Specifically with the GLOBAL_ACTION_HOME and GLOBAL_ACTION_BACK actions)
Of course you will need appropriate permissions for an Accessibility Service, but this does not require root
Here are some precision to Roman answer
There is also
InputConnection
'ssendKeyEvent
function.InputConnection
is only API Level 3.It works for me:
You can try this.
This code can work for me.
Note : Please remember to replace the "R.id.MainActivity" to your Activity name.