我正在开发Android应用程序,但我一时陷入困境,
我想做的是
当用户一天中第一次启动该应用程序时,我想向他显示一些警报。当他在同一天第二次打开应用程序时,不会收到警报。(他只会在当天首次启动应用时收到警报)。
第二天,如果他再次第一次打开该应用程序,他将收到警报,第二次他将不会收到警报。
简而言之:用户应在每天的首次启动时获得警报。
任何想法,我应该如何做到这一点?提前致谢。
我们可以通过共享首选项来实现。在您的第一个活动中,有一个方法可以在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()));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句