public class CountTimer extends CountDownTimer {
private Context context;
public CountTimer(long millisInFuture, long countDownInterval, Context context) {
super(millisInFuture, countDownInterval);
this.context = context;
}
// 计时完毕时触发
@Override
public void onFinish() {
context.startActivity(new Intent(context, MainActivity.class));
}
// 计时过程显示
@Override
public void onTick(long millisUntilFinished) {
}
}
下面的写页面里面
public class baseDispatchTouchActivity extends AppCompatActivity{
private CountTimer countTimerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void timeStart(){
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
countTimerView.start();
}
});
}
private void init() {
//初始化CountTimer,设置倒计时为2分钟。
countTimerView=new CountTimer(120000,1000,baseDispatchTouchActivity.this);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
//获取触摸动作,如果ACTION_UP,计时开始。
case MotionEvent.ACTION_UP:
countTimerView.start();
break;
//否则其他动作计时取消
default:countTimerView.cancel();
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
protected void onPause() {
super.onPause();
countTimerView.cancel();
}
@Override
protected void onResume() {
super.onResume();
timeStart();
}
}
转载Android关于界面一定时间无操作自动跳转到指定界面的实现_Chen_xiaobao的博客-CSDN博客



