How to create an automatic click (touch) on the Android screen?

0

I'm a beginner and I'm trying to make a game and I want to automatically, during the execution of the app, click on a specific area of the screen as if the user had done it.

1 - Can I simulate this click / tap automatically without a real user click?

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

Thank you in advance.

    
asked by anonymous 12.01.2018 / 20:04

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 Random class of Java.

Source: link

    
18.01.2018 / 18:18