在android上创建会话并在特定时间内销毁会话

在android上创建会话并在特定时间内销毁会话,android,session,time,Android,Session,Time,我在stackoverflow中尝试了所有类似的问题,但仍然没有给我正确的答案(比如我要找的) 我尝试在一天内创建一个会话。会话将在特定时间内销毁 private void notifyPopUpWhen24Hours(){ Calendar firingCal= Calendar.getInstance(); Calendar currentCal = Calendar.getInstance(); firingCal.set(Calendar

我在stackoverflow中尝试了所有类似的问题,但仍然没有给我正确的答案(比如我要找的)

我尝试在一天内创建一个会话。会话将在特定时间内销毁

private void notifyPopUpWhen24Hours(){
        Calendar firingCal= Calendar.getInstance();
        Calendar currentCal = Calendar.getInstance();

        firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
        firingCal.set(Calendar.MINUTE, 0); // Particular minute
        firingCal.set(Calendar.SECOND, 0); // particular second

        Date currentDates = currentCal.getTime();


        if(currentDates.before(firingCal.getTime())){
            //do simething
            // the session still on
        }else{
            // If the current date is after the firingCal.getTime, then sesion destroy
        }

    }
在上面的代码中,我尝试每早上8点销毁一次会话。 但是上面的代码没有给出我想要的

我的问题是,如何创建会话并在特定时间内销毁它。谢谢。

所以您需要一个操作(假设它是removeSession()),该操作将在每天早上8点执行。请使用
AlarmManger
BroadcastReceiver
执行相同操作。你可以用这个