gpt4 book ai didi

java - 是否可以将事件从线程发送到 Activity ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:45 26 4
gpt4 key购买 nike

如果我想发送一个事件,例如OnClick,从一个线程到一个 Activity ?谢谢。

预期的工作流程如下:

public class HelloAndroid extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Crate threadA
Start threadA
}

public void OnSomeEvent() {
do something that changes the views in this activity;
}

private class ThreadA extends Thread {
public void run() {
do something ...

Send Some Event to Activity HelloAndroid.
}
}

最佳答案

您始终可以从线程向 Activity 发送消息,如下所示:

//this should be in your Activity class
private Handler SomeHandler = new Handler() {
public void handleMessage(Message msg) {
ReactOnMessage();
}
};


private class SomeThread implements Runnable {
public void run() {
doSomething();
SomeHandler.sendEmptyMessage(0);
}
}

您还可以创建包含一些值的消息。

关于java - 是否可以将事件从线程发送到 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2151754/

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