android app实现定时退出主界面

452 阅读1分钟

给批发市场做了一个蔬菜机app,希望在晚上12点的时候能自动退出主界面
到登录界面,使用Timer定时器实现
在onCreate()

SimpleDateFormat nowtime = new SimpleDateFormat("hh:mm:ss");
        Date date = new Date();
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                date.setTime(System.currentTimeMillis());
//                Log.e("wy","当前时间:"+nowtime.format(date));
//                23:59:59
               if(nowtime.format(date).equals("23:59:59")){
                   startActivity(new Intent(TradeActivity.this,LoginActivity.class));
                   finish();
               }
            }
        },0,1000);

在onDestory

mTimer.cancel();