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

Mr *_*ice 3 android android-alertdialog android-activity

我正在开发Android应用程序,其中我卡在一点,

我想做的是,

当用户在一天内第一次启动应用程序时,我想向他显示一些警报.当他在同一天第二次打开应用程序时,它将不会收到警报.(他只会在第一天发布应用程序时收到警报).

第二天,如果他再次第一次打开应用程序,他会得到警报,第二次他不会得到警报.

简而言之:用户应该在每天的首次发布时获得警报.

任何想法,我该怎么做到这一点?提前致谢.

Nis*_*hia 11

我们可以通过共享偏好来实现这一目标.在您的第一个活动中,有一个方法在oncreate方法中执行以下一步一步:

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.
Run Code Online (Sandbox Code Playgroud)

示例代码:

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()));
}
Run Code Online (Sandbox Code Playgroud)