public class MainActivity extends AppCompatActivity { private Handler mHandler = new Handler(); private final Runnable mGotoOtherPageTask = new Runnable() { @Override public void run() { gotoOtherPage(); } }; private Button start_screen_Saver; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 当前界面打开时,设置一个延迟自动执行的跳转任务 postDelayTask(); start_screen_Saver = (Button) findViewById(R.id.start_screen_Saver); start_screen_Saver.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } @Override public boolean dispatchTouchEvent(MotionEvent event) {//此方法会检测到用户操作,但不会把点击事件吃掉,点击事件依然会正常执行下去 int action = event.getAction() & MotionEvent.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: // 当发生触摸事件时,重新设置自动任务 postDelayTask(); break; default: break; } return super.dispatchTouchEvent(event); } private void postDelayTask() { // 先将之前的任务移除,如果存在的话 mHandler.removeCallbacks(mGotoOtherPageTask); // 延迟5000毫秒执行跳转任务 mHandler.postDelayed(mGotoOtherPageTask, 5000); } /** * 跳转到其他界面 */ private void gotoOtherPage() { Intent intent = new Intent(this, Main2Activity.class); startActivity(intent); } }
时间: 2024-10-06 13:10:41