Question

what i'm trying to do is trigger an ontouch event at a specific region on my activity when i receive a specific string from an arduino(via bluetooth). i trying to build a controller with a wii and an arduino to use in a game i'm writing myself.

before you answer this , i already know that there is a function * openButton.performClick(); for this but in the game i'm not always going to be using buttons so its not good .

i want to simulate a touch like the the adb does it with monkey, but without root permission something like inject touch just no root.

this is part of the code:

    int[] loc = new int[2]; 
    openButton.getLocationOnScreen(loc);// get the region where to simulate the //touch event
    xTest = (float)loc[0]; 
    yTest = (float)loc[1]; 



@SuppressLint("Recycle") void testing(){
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 100;
    float x = xTest;
    float y = yTest;


    int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(
        downTime, 
        eventTime, 
        MotionEvent.ACTION_UP, 
        x, 
        y, 
        metaState
    );
    dispatchTouchEvent(motionEvent);


}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
     int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                // do something
                openButton.performClick();
                break;
            case MotionEvent.ACTION_MOVE:
                // do something
                // how to trigger a ACTION_DOWN event here?
                break;
    }

    return false;
}

so wenn i get a specific string i want to simulate a toucht event programmatically at a specific region on the screen, this time its a button , but it could be something else thats why "view.performclick()" is not good. I've seen some examples where before the dispatchTouchEvent they put view, if i do this i get an error thanks you in advance for your help

Was it helpful?

Solution

If you're using an onTouchListener, the onTouch(...) method is public so I guess you could call it.

So when you instantiate the onTouchListener store it as a variable.

OnTouchListener listener = new OnTouchListener() { [[override code etc.]]  }

Then just call the onTouch method you've overridden and give it the view and event you want to simulate the touch on.

MotionEvent doesn't have a contractor, but has a factory method called obtain():

MotionEvent myEvent = MotionEvent.obtain(long downTime, long eventTime, int action, float x, float y, int metaState);

Then just pass it to your onTouch:

onTouch(myView, myEvent);

Assuming you've passed the correct values to your event, it won't know the difference.

For more on the MotionEvent and what the parameters mean, go to:

http://developer.android.com/reference/android/view/MotionEvent.html

Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top