简体   繁体   English

我需要在每个星期五展示祝酒词

[英]I need to show a toast message on every Friday

I want to show a text message in a textview on every friday of the week, and hide it all other days, it displays the whole day on fridays, and hide on all other days. 我想在一周的每个星期五在textview中显示一条短信,然后将其隐藏在其他所有日子中,将其显示在星期五的全天,并在其他所有日子都隐藏。 How can i implement this, I have not tried any code since i do not know where to start from. 我该怎么实现呢,因为我不知道从哪里开始,所以我没有尝试任何代码。 Any example code? 任何示例代码?

perhaps store the last time the message was displayed in the app's preferences, and then only display the message on Fridays if the previous display time was on a different day. 可能会在应用程序的首选项中存储上一次显示消息的时间,然后如果前一个显示时间是在另一天,则仅在星期五显示消息。

Calendar calendar = Calendar.getInstance();

if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Calendar noticeDisplayTime = Calendar.getInstance();
    noticeDisplayTime.setTimeInMillis(prefs.getLong("noticeDisplayTime", 0));

    if (!(calendar.get(Calendar.DAY_OF_YEAR) == noticeDisplayTime.get(Calendar.DAY_OF_YEAR) &&
        calendar.get(Calendar.YEAR) == noticeDisplayTime.get(Calendar.YEAR)) {
        prefs.edit().putLong("noticeDisplayTime", calendar.getTimeInMillis()).apply();
        // Display the notice
    }
}

Better use WorkManager to schedule a PeriodicWorkRequest which displays a Notification (in order to provide you with the proper search terms). 最好使用WorkManager安排一个PeriodicWorkRequest来显示Notification (以便为您提供适当的搜索词)。 I use this to test my WorkManager schedules, which do not display any notifications in release mode. 我用它来测试我的WorkManager计划,该计划在发布模式下不显示任何通知。 The reason is, that Toast is always silent - while these I can hear, eg. 原因是, Toast始终保持沉默-例如,我可以听到这些声音。 even when streaming music, because the stream audio ducks away. 即使在流音乐时,因为流音频也会消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM