在您的情况下,我建议使用 Handler 类。这是我要做的:
private static class PromoScroller implements Runnable {
private Handler _scrollHandler;
public PromoScroller(Handler scrollHandler) {
_scrollHandler = scrollHandler;
}
@Override
public void run() {
// ..
_scrollHandler.sendEmptyMessage(0);
}
}
// 在你的活动中的某个地方:
_promoScroller = new PromoScroller(new Handler() {
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
// !! catch message and start the activity
Intent = new Intent(YourCurrentActivty.this, YourTargetActivity.class);
}
});
_scrollerThread = new Thread(_promoScroller);
_scrollerThread.start();
PS这些是我用于滚动计时器的代码,但你明白了
UPD
// TASK
public class YourTimerTask extends TimerTask {
private Handler _Handler;
public YourTimerTask(Handler handler) {
_Handler = handler;
}
public void run() {
_Handler.sendEmptyMessage(0);
}
}
// TASK HANDLER (private property in your acitivity)
private Handler _taskHandler = new Handler(){
public void dispatchMessage(android.os.Message msg) {
// do cleanup, close db cursors, file handler, etc.
// start your target activity
Intent viewTargetActivity = new Intent(YourCurrentActivity.this, YourTargetActivity.class);
};
};
// IN YOUR ACTIVITY (for isntance, in onResume method)
Timer timer = new Timer();
timer.schedule(new YourTimerTask(_taskHandler), seconds*1000);
这应该可以完成这项工作。对于计时器 - 只是谷歌。计时器示例
UPD2
我的错误-应该是Handler _timerHandler = ...
。开始活动看这里