Android 如何从共享首选项中删除数据

Android 如何从共享首选项中删除数据,android,android-sharedpreferences,Android,Android Sharedpreferences,我在程序中使用共享首选项。在我的程序中有几个按钮,每个按钮上都有一个值,但我想在特定的时间内删除它们,但我无法提供帮助 Calendar cal = Calendar.getInstance(); //Create Calendar-Object cal.setTime(new Date()); //Set the Calendar to now int hour = cal.get(Calendar.HOUR_OF_D

我在程序中使用共享首选项。在我的程序中有几个按钮,每个按钮上都有一个值,但我想在特定的时间内删除它们,但我无法提供帮助

Calendar cal = Calendar.getInstance(); //Create Calendar-Object
        cal.setTime(new Date());
                     //Set the Calendar to now

        int hour = cal.get(Calendar.HOUR_OF_DAY); //Get the hour from the calendar
        if(hour > 23)              
        {

            SharedPreferences preferences = getSharedPreferences("Count", 0);
            preferences.edit().remove("IntValue_").apply();
            editor.clear();
            editor.commit();
        }

我认为Calendar.HOUR OF_DAY的范围为0-23,因此您永远无法从
(HOUR>23)

中获得真实值。请编辑您的问题以包括异常堆栈跟踪,不清楚错误是什么。请仔细阅读并解释“但我不能”是什么意思。我的意思是我在很多方面都试过了。这是我最后一次尝试。它不会从按钮中删除值。我认为您的首选项存在冲突。编辑()和
editor
以及
commit
apply
在您的if条件下尝试使用hour<1。目前,一天中的小时数从未超过23,因为它在24时变成00:00