监听用户无操作之后跳转到另一个页面

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paper_change);

        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        initView();
        image.setImageResource(R.drawable.renminribao);

    }
//new一个handle来计算无操作的时间
    private Handler mHandler = new Handler();
    private final Runnable mGotoOtherPageTask = new Runnable() {
        @Override
        public void run() {
            gotoOtherPage();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        // 当前界面打开时,设置一个延迟自动执行的跳转任务
        postDelayTask();
    }
    @Override
    protected void onStop() {
        super.onStop();
        mHandler.removeCallbacks(mGotoOtherPageTask);//移除任务
    }

    
    @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 gotoOtherPage() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void postDelayTask() {
        // 先将之前的任务移除,如果存在的话
        mHandler.removeCallbacks(mGotoOtherPageTask);
        // 延迟5000毫秒执行跳转任务
        mHandler.postDelayed(mGotoOtherPageTask, 5000);
    }

 

posted @ 2017-09-10 17:28  劳猿外  阅读(563)  评论(0编辑  收藏  举报