我有一个线程处理程序接收消息的问题。在我实现此模式的所有其他线程中,一切都正常。这是我的代码:
启动线程
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中,但是处理程序中的“消息已接收”日志没有出现。