我可以通过从另一个线程调用onTouch()方法安全地将触摸事件发送到主活动吗?

时间:2012-03-05 03:45:09

标签: android testing android-activity ontouchevent monkey

请告诉我这种方法是否安全或我可以使用什么而不是普通的线程来安全地将Touch事件发送到我的活动,以便从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?
    }
}

1 个答案:

答案 0 :(得分:1)

使用单身类

Singleton architecture