1

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

Community
  • 1
  • 1
Fish
  • 1,689
  • 1
  • 18
  • 28
  • "I tried it on multiple games and even my own application" -- it's certainly not going to work on any other application. You cannot send fake touch input to another app this way. At best, this sort of thing might work on your own app. – CommonsWare May 29 '15 at 14:18
  • Thanks, but I want it to work on other applications. Is there a method? Or It is just that there isn't a method to do such a thing? – Fish May 29 '15 at 15:45

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491