How to create an automatic click (tap) on your Android screen?

Asked

Viewed 1,042 times

0

I am a beginner and I am trying to make a game and wish that automatically, during the execution of the app occurs a click on specific area of the screen, as if the user had done.

1 - I can simulate this click/touch automatically, without a real user click?

2 - If yes, is there any way to make click/tap on the random screen?

thank you in advance.

1 answer

1

view.setOnTouchListener(new OnTouchListener()
{
    public boolean onTouch(View v, MotionEvent event)
    {
        Toast toast = Toast.makeText(
            getApplicationContext(), 
            "View touched", 
            Toast.LENGTH_LONG
        );
        toast.show();

        return true;
    }
});


// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
    downTime, 
    eventTime, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState
);

// Dispatch touch event to view
view.dispatchTouchEvent(motionEvent);

You can generate random numbers with the class Random java.

Source: https://stackoverflow.com/questions/4396059/how-to-simulate-a-touch-event-in-android

Browser other questions tagged

You are not signed in. Login or sign up in order to post.