gpt4 book ai didi

Android 线程处理程序未收到消息

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:58 25 4
gpt4 key购买 nike

我遇到了线程处理程序接收消息的问题。我实现此模式的所有其他线程都可以正常工作。这是我的代码:

开始线程

InternalScoresThread t = new InternalScoresThread(
this.game.getApplicationContext(),
this.map.fileName, this.map.getCurrentTime(),
new Handler() {

@Override
public void handleMessage(Message msg) {

Log.d("DEBUG", "message received");

if (msg.getData().getBoolean("record")) {

Player.this.showtRecordMessage();

} else {

Player.this.showtFinishMessage();
}

Player.this.showTimeMessage();
Player.this.showRestartMessage();
}
});

t.start();

线程类

public class InternalScoresThread extends Thread {

private Handler handler;
private String map;
private float time;
private Context context;

public InternalScoresThread(Context context, String map, float time, Handler handler) {

this.context = context;
this.handler = handler;
this.map = map;
this.time = time;
}

@Override
public void run() {

Log.d("DEBUG", "thread started");

Database db = Database.getInstance(this.context);
float bestTime = db.getBestTime(this.map);
db.addRace(this.map, this.time);

Log.d("DEBUG", "race added");

Message msg = new Message();
Bundle b = new Bundle();
b.putBoolean("record", this.time < bestTime || bestTime == 0);
msg.setData(b);
this.handler.sendMessage(msg);

Log.d("DEBUG", "message sent");
}
}

“线程启动”、“比赛添加”和“消息发送”日志出现在 logcat 中,但不出现在处理程序中的“消息接收”。

最佳答案

好吧,我不知道为什么,但是 dispatchMessage() 而不是 sendMessage() 解决了问题...

关于Android 线程处理程序未收到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9992001/

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