2012-03-05 40 views
1

請告訴我,如果這種方法是安全的,或者我可以使用而不是一個普通的線程來安全地將觸摸事件分配給我的活動,目的是從該onTouch()方法測試整個流程女巫觸發器。我可以通過從另一個線程調用onTouch()方法安全地將觸發事件分派給主要活動嗎?

//this is the thread which fires Touch Events to my main activity 
public class Monkey extends Thread { 

    Run r; 
    float x,y; 
    public Monkey (Run a) 
    { 
     r = a; 
    } 

    public void run() 
    { 
     int i = 0; 
     while(i<10000) 

     { 
      r.onTouch(r.geView(), 
       MotionEvent.obtain(SystemClock.uptimeMillis(), 
       SystemClock.uptimeMillis(), 2, x++, y++, 0)); 
     } 
    } 
} 

// and this is the main activity 
public class Run extends Activity implements OnTouchListener{ 
    private GLSurfaceView glSurface; 

    public View geView() 
    { 
     return glSurface; 
    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     glSurface = new GLSurfaceView(this); 
     glSurface.setOnTouchListener(this); 
     setContentView(glSurface); 
     m = new Monkey(this); 
    } 

    public boolean onTouch(View v, MotionEvent event) 
    { 
     //if from here i make other calls to classes that update my glsurface will my application eventualy crash? 
    } 
} 

回答

相關問題