2015-11-07 60 views
0

我做了什麼我能夠使用this post模擬觸摸事件在一定間隔的Android

// Obtain MotionEvent object 
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; 
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() 
int metaState = 0; 
MotionEvent motionEvent = MotionEvent.obtain(
    downTime, 
    eventTime, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState 
); 

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

什麼,我試圖做模擬觸摸事件:

  1. 我哇nt以每5秒爲間隔模擬觸摸事件。
  2. 如何實現這個

回答

0
// Obtain MotionEvent object 
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; 
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() 
int metaState = 0; 
MotionEvent motionEvent = MotionEvent.obtain(
    downTime, 
    eventTime, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState 
); 

// Dispatch touch event to view 
Timer timer = new Timer(); 
     new Timer().schedule(new TimerTask() { 
      @Override 
      public void run() { 
     runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      view.dispatchTouchEvent(motionEvent); 
     } 
     }); 


      } 
     },0,5000); 
+0

試過了,收到錯誤無法裏面looper.prepare創建處理程序..... – Devrath

+0

嘗試在UI線程。 Plz向我顯示了整個錯誤信息。 –