我有每 1 秒安排一次的计时器,由于我们不能在 timertask 中敬酒,所以我使用了 handler.post()。但是这段代码破坏了我的应用程序:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String text = (String) msg.obj;
sec_view.setText(text);
}
};
MyTimerTask myTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTask, 1000, 1000);
}
Runnable makeToast = new Runnable() {
public void run() {
Toast.makeText(null, "qwerty", Toast.LENGTH_LONG).show();
}
};
class MyTimerTask extends TimerTask {
public void run(){
if(0 == --sec){
handler.post(makeToast); //breaks there
timer.cancel();
}
Message msg = new Message();
msg.obj = sec+" sekund";
handler.sendMessage(msg);
}
}
我可以使用其他任何东西来从 timertask 敬酒吗?