I am developing Android app in which i got stuck at one point,
What i want to do is,
When user launches the app for the first time in a day, i want to show him a some alert. And when he opens a app for the second time in same day it will not get an alert. (he will get an alert only for the first launch of app in day).
next day again if he opens the app for the first time again he will get alert and on second time he will not get an alert.
In short: User should get alert on first launch of each day.
Any idea, how should i achieve this ? Thanks in advance.
We can achieve this via a shared preference.
In your first activity, have a method which does the following step by step in 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.
Sample code:
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()));
}
A more explicit example of Nishanthi's solution that works great for me:
SharedPreferences sharedPref = getSharedPreferences(PREFS_NAME, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String currentDate = sdf.format(new Date());
if (sharedPref.getString("LAST_LAUNCH_DATE","nodate").contains(currentDate)){
// 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...
new CheckUpgradeStatus().execute();
// Set the last Launched date to today.
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("LAST_LAUNCH_DATE", currentDate);
editor.commit();
}
- Implement function getCurrentDate() in DateHelper class.
- Implement function set and get in SharedPreference class.
- Implement function for check current date.
Please see below code.
1. DateHelper class.
public static String getCurrentDate() {
DateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
return simpleDateFormat.format(date);
}
2. SharedPreference class.
public String getDatePreference() {
SharedPreferences preferences = getPreference();
return preferences.getString(DATE_KEY, null);
}
public void setDatePreference(String datePreference) {
final SharedPreferences.Editor editor = getEditor();
editor.putString(DATE_KEY, datePreference);
editor.apply();
}
3.Implement function showMessageOnceDay in Activity class.
public showMessageOnceDay(String message) {
currentDate = sharedPreference.getDatePreference();
if (currentDate == null) {
alertDialogMessage(message);
sharedPreference.setDatePreference(DateHelper.getCurrentDate());
} else {
if (!currentDate.equals(DateHelper.getCurrentDate())) {
alertDialogMessage(message);
sharedPreference.setDatePreference(DateHelper.getCurrentDate());
}
}
}
SharedPrefrences
will help you to accomplish your task.While opening the application just check the value of some boolean
variable from the shared preferences.Set an alarm using AlarmManager
which set the value of some boolean
variable to false
everyday in your sharedpreferences
at 00:00
through a service and then change the value of that variable to true
when the user opens your application.If the variable is already true
then nothing will happen for the rest of the day .In actual you have to check the value every time your application starts,nothing must turn that variable to false except that service that will run at 00:00
for shared preferences see this
How to use SharedPreferences
for alarm manager
see this Alarm Manager Example
It is not a good solution, but I won't show message until user kill the app. Here is my code:
private static boolean isFirstLaunch = true;
if (isFirstLaunch){
//show dialog
isFirstLauch = false;
}