-2

I'm implementing the concept of if today date is present one alert message show one time only.

Example 5-8-14 only once toast message show.

6-8-14 only once toast message show ' in every date only once toast show.

Edit: Only after first start of App, a toast with current date should appear. If I start my app second or third time, then there should no toast appear

dominic.e
  • 286
  • 3
  • 17

2 Answers2

0

Logic:

What:
1. When ever you show the toast, Save the date/day in sharedPreferences.
2. Then each time, compare the value of "today's date" with the sharedPref value, if its different, show the toast.

How:
- Make a function for the toast, inside which update the Shared Pref value.
- In an "if" loop, compare the default shared pref value - so when you run the app for the first time, it'l return the default value and enter the show toast function.

Eg.

if(!(sharedPrefSavedDate.equals(sharedPrefDefaultValue))){
if(!(String.valueOf(new SimpleDateFormat("dd").format(new java.util.Date())).equals(sharedPrefSavedDate)){
                             showToast();
}
}  

Inside showToast(), save the value of today's date in Shared Preferences.

The saved value can be integer or String. Can change .equals to != of = accordingly.

May require:
How to use shared preferences

Example with integer on android developers

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
0

Look into sharedPreferences. Here a Link to android developer: sharedPreferences

Set a bool-value after first start and after each start check this value and if it's true, then don't show toast

GetDate:

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy");
String strDate = sdf.format(c.getTime());

Link: Get current time and date

Write:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBool("Toast", True);
editor.putString("Date", strDate);
editor.commit();

Read:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
bool bToast = sharedPref.getBool("Toast", False);
String strDate = sharedPref.getString("Date", null);
dominic.e
  • 286
  • 3
  • 17
  • and when will the bool-value be false? requirement is such that it would need to check date, not boolean value :) – Pararth Aug 05 '14 at 06:57