5

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.

Mr Nice
  • 524
  • 8
  • 24
  • Android has SharedPreferences, this is persistent memory. You can store and get the date (And check if the day has changed) – eddie Jul 24 '14 at 06:18
  • ok.but i want to know how to detect first launch of each day – Mr Nice Jul 24 '14 at 06:20

5 Answers5

12

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()));
}
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • @MrNice: It is just a variable name. Now i have changed as lastvisited date. Wait I shall upload relevant code too.. – ngrashia Jul 24 '14 at 06:24
  • @MrNice: LastLaunch Date would be null on the first time user opens app after installation. So, it will enter the else loop. So, current date will be saved as last launch date as per the code (`sharedPref.saveSharedPreference`). From next time onwards, if current date is same as saved date, it will enter the if block. If current date is not same as saved date, it will enter else block. – ngrashia Jul 24 '14 at 06:37
  • Awesome, i thought of same work with shared prefs + workmanager (prob alarm manager), but your way reduces much lines of code. – Anshul1507 Jul 22 '20 at 11:07
2

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();
        }
Will Evers
  • 934
  • 9
  • 17
2
  1. Implement function getCurrentDate() in DateHelper class.
  2. Implement function set and get in SharedPreference class.
  3. 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());
        }
    }
    }
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Chattip Soontaku
  • 560
  • 1
  • 6
  • 17
0

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

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
0

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; }

K.Sopheak
  • 22,904
  • 4
  • 33
  • 78