Reputation: 217
I've been trying to figure out how to inject touch/ keyboard events into an Android device for a while now (within and outside of you application).
I found an app that does this without root permissions:
https://play.google.com/store/apps/details?id=com.vmlite.vncserver
Does anyone have any clue how they did it?
Upvotes: 4
Views: 5983
Reputation: 2209
If you want to inject touch events on android app without root:
you can use Instrumentation class, https://developer.android.com/reference/android/app/Instrumentation.html
import android.app.Instrumentation;
public class MainActivity extends Activity {
Instrumentation m_Instrumentation;
public void onCreate(Bundle savedInstanceState) {
m_Instrumentation = new Instrumentation();
int x=0; //your x coord in screen.
int y=0; // your y coord in screen.
m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,x, y,0));
m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,x, y,0));
}
}
This method obtains ACTION_DOWN and ACTION_UP event which injects an event on the current layout.
Note: if the injection of your coordinates (x,y) is outside of screen size, the app will crash. This method injection works only inside app, if you want to inject touch events, you need a rooted device and inject events through adb command.
Upvotes: 3
Reputation: 23961
for non-rooted devices, every time after turning the device completely off and on, you will have to connect your device to a Windows PC or Mac using a USB cable, then run a free desktop program, VMLite Android App Controller, to start the server on your device.
I'm quite sure that at this step it changes the permissions on /dev/input/event..
files and performs the injecting by writing those (sidestepping the Android VM). This technique is detailed in this blog post: Part1, Part2
Upvotes: 2