android - 在 android 中每天首次启动应用程序时显示警报

标签 android android-activity android-alertdialog

我在开发 Android 应用程序时遇到了困难,

我想做的是,

当用户在一天中第一次启动该应用程序时,我想向他显示一些警报。当他在同一天第二次打开应用程序时,它不会收到警报。 (他只会在当天首次启动应用程序时收到提醒)。

第二天,如果他再次打开该应用程序,他将收到警报,而第二次他将不会收到警报。

简而言之:用户应该在每天首次启动时收到提醒。

任何想法,我应该如何实现这一点?提前致谢。

最佳答案

我们可以通过 shared preference 来实现这一点. 在您的第一个 Activity 中,有一个方法在 oncreatemethod 中逐步执行以下操作:

1. Read a value (lastlaunchdate) from shared preference.

2. Check if current date = lastlaunchdate

3. If #2 is true, then ignore and proceed with usual flow

4. If #2 is false, then  

 4.a display the alert box

  4.b save current date as lastlaunchdate in shared preference.

示例代码:

if (sharedPref.loadSharedPreference(getApplicationContext(), "LAST_LAUNCH_DATE").equals(new SimpleDateFormat("yyyy/MM/dd", Locale.US).format(new Date())))
{
    // Date matches. User has already Launched the app once today. So do nothing.
}
else
{
    // Display dialog text here......
    // Do all other actions for first time launch in the day...
    // Set the last Launched date to today.
    sharedPref.saveSharedPreference(getApplicationContext(), "LAST_LAUNCH_DATE", new SimpleDateFormat("yyyy/MM/dd", Locale.US).format(new Date()));
}

关于android - 在 android 中每天首次启动应用程序时显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926369/

相关文章:

android - 公共(public)静态变量或使用意向类的 putExtra 方法

android - 如何更改 fragment 的大小?

android - AlertDialog 样式 - 如何更改标题、消息等的样式(颜色)

flutter - 如何在 Flutter 的 AlertDialog 中实现 Slider?

android - 警报对话框背景主题/颜色

java - 在日期选择器中显示两次的日期

java - 从另一个类更新 ListView 项目

android - 图像未加载到 ImageView 中

java - 当应用程序从后台转到前台时,onBackPressed() 不起作用

java - 我如何在java中发送http请求?