java:
private long startTime;
private boolean touched = false;
private Timer timer;
//自動跳轉加上Touch
//開啟定時器
timer = new Timer(true);
startTime = System.currentTimeMillis();
timer.schedule(task, 0,1);
private final TimerTask task = new TimerTask() {
@Override
public void run() {
if (task.scheduledExecutionTime() - startTime == 5000 || touched) { //5秒後或是在5秒內觸碰螢幕將跳轉
Message message = new Message();
message.what = 0;
timerHandler.sendMessage(message);
timer.cancel();
this.cancel();
}
}
};
private final Handler timerHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
//跳轉
MainActivity.this.finish();
Intent intent = new Intent(MainActivity.this, LogingActivity.class);
MainActivity.this.startActivity(intent);
break;
}
super.handleMessage(msg);
}
};
//點選直接跳轉
public boolean onTouchEvent(MotionEvent event){
if(event.getAction()==MotionEvent.ACTION_DOWN){
touched = true;
}
return true;
}
此程式設定在5秒以上會有BUG,停留在主頁面!!
留言列表