Pergunta

I want to make a MotionEvent to controll a webiew. If the api is lower than 16 everything works with:

 long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 500;
    float x = 50f;
    float y = 50f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    int metaState = 0;
    MotionEvent me = MotionEvent.obtain(
        downTime, 
        eventTime, 
        action, 
        x, 
        y, 
        metaState
    );

   view.dispatchTouchEvent(me);

But is the api higher, then I get an error with: Source was not SOURCE_CLASS_PIONTER. Then I changed the code into:

long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 500;
    float x = 50f;
    float y = 50f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    int metaState = 0;
    MotionEvent.PointerCoords p[] = null; 
    MotionEvent.PointerProperties p2[] = null;
  p[0] = new MotionEvent.PointerCoords();
p[0].x = x;
p[0].y = y;
p2[0] = new MotionEvent.PointerProperties();
p2[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
p2[0].id = 0;
MotionEvent me =  MotionEvent.obtain(downTime, eventTime, action, 1, p2, p, metaState, 0, 1f, 1f, 0,0, 0, 0);
    view.dispatchTouchEvent(me);

But it did not work. Do you have can help me? Sorry for my bad English.

Greetings from Germany.

Foi útil?

Solução

  MotionEvent me = MotionEvent.obtain(
        downTime, 
        eventTime, 
        action, 
        x, 
        y, 
        metaState
    );
    me.setSource(4098);
   view.dispatchTouchEvent(me);

Works for me.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top