我正在开发 Android 应用程序,其中我遇到了一个问题,
我想做的是,
当用户一天中第一次启动该应用程序时,我想向他显示一些警报。当他在同一天第二次打开应用程序时,它不会收到警报。 (他只会在当天首次启动应用程序时收到警报)。
第二天,如果他再次第一次打开应用程序,他会收到警报,第二次他不会收到警报。
简而言之:用户应该在每天第一次启动时收到警报。
任何想法,我应该如何实现这一目标?提前致谢。
我们可以通过共同偏好 http://developer.android.com/reference/android/content/SharedPreferences.html。
在您的第一个活动中,有一个方法在 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()));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)