gpt4 book ai didi

android - 我可以通过从另一个线程调用 onTouch() 方法来安全地将触摸事件分派(dispatch)给主要 Activity 吗?

转载 作者:行者123 更新时间:2023-11-28 20:46:05 24 4
gpt4 key购买 nike

请告诉我这种方法是否安全,或者我可以使用什么代替普通线程来安全地将触摸事件分派(dispatch)到我的 Activity ,以便测试从该 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?
}
}

最佳答案

利用单例类

Singleton architecture

关于android - 我可以通过从另一个线程调用 onTouch() 方法来安全地将触摸事件分派(dispatch)给主要 Activity 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9561383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com