メッセージを受信しているスレッドハンドラに問題があります。このパターンを実装した他のすべてのスレッドはうまく動作します。ここに私のコード:Androidスレッドハンドラがメッセージを受信していません
スタートスレッド
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");
}
}
「スレッドが起動は、 "レースは追加Threadクラス" と "送信されたメッセージは、" ログは、logcatに表示されますが、ハンドラで "メッセージを受け取った"ではありません。
これはどうして起こったのか他の説明はありますか?私はこの問題を約50%の時間に見ています – JuJoDi
dispatchMessage()はhandleMessage()を同じスレッドで直接呼び出すため、スレッド間の通信の問題を実際には解決しません。 – Memetic