I want to fake a touch event and I have got the possible answer here. However When I attempted to make it work, it didn't work.
Note that I am running the following code in the thread.
private Handler handler = new Handler(Looper.getMainLooper());
private final Runnable runnable = new Runnable() {
@Override
public void run() {
View ParentView = (View)view.getRootView();
long downTime;
long eventTime;
Log.v("Screen Tapper", "Start Tapping");
Log.v("Screen Tapper", "tapTimes ----- "+1);
downTime = eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x-5, y-5, 0);
ParentView.onTouchEvent(event);
Log.v("Screen Tapper", "touchDown ----- "+x+","+y);
handler.postDelayed(runnable, 100000);
downTime = eventTime = SystemClock.uptimeMillis();
MotionEvent event2 = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x-5, y-5, 0);
ParentView.onTouchEvent(event2);
Log.v("Screen Tapper", "touchUp ----- "+x+","+y);
handler.postDelayed(runnable, 100000);
}
};
And when I want to start or stop the thread I basically call
Handler.post(runnable);
or
Handler.removeCallBack(runnable);
However, This method is not working.
I tried it on multiple games and even my own application. I know this thread is running because the logging is working. However the button is just not being pressed.
Any help is appreciated
It is not possible to send arbitrary fake touch events to other applications, except perhaps on rooted devices. Even there, it would be through fussing with low-level Linux input stuff AFAIK, not through the Android SDK.