Skip to content Skip to sidebar Skip to footer

Android - How Can A Service App Dispatch Keyevents To Another Foreground App

I want to develop an Android Service app that dispatches KeyEvents to the foreground application. Somehow, it would be like this I cant seem to find a way how, I am only familiar

Solution 1:

You can definitely do this with root only and without installing your application as a system app.

The way this can be accomplished is by using uiautomator framework. This is such a powerful framework available for android with the purpose of black box testing applications.

Obviously you are going to use this framework for a slightly different purpose than black box testing, but that's ok.

UiDevice has the method pressKeyCode(int keyCode) that can be used like this to send input to whatever app happends to be in the forground:

UiDevicedev= UiDevice.getInstance();
dev.pressKeyCode(KeyEvent.KEYCODE_1);

The setup to use uiautomator can be a little complex, see this other answer of mine where I describe in more detail what you need to do.

Post a Comment for "Android - How Can A Service App Dispatch Keyevents To Another Foreground App"