Skip to content Skip to sidebar Skip to footer

Show Alert On First Launch Application Of Each Day In Android

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 wh

Solution 1:

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) fromshared preference.

2. Check if current date = lastlaunchdate

3. If #2istrue, then ignore and proceed with usual flow

4. If #2isfalse, then4.a display the alert box

  4.b save current dateas lastlaunchdate inshared preference.

Sample code:

if (sharedPref.loadSharedPreference(getApplicationContext(), "LAST_LAUNCH_DATE").equals(newSimpleDateFormat("yyyy/MM/dd", Locale.US).format(newDate())))
{
    // 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", newSimpleDateFormat("yyyy/MM/dd", Locale.US).format(newDate()));
}

Solution 2:

A more explicit example of Nishanthi's solution that works great for me:

SharedPreferencessharedPref= getSharedPreferences(PREFS_NAME, 0);
        SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
        StringcurrentDate= sdf.format(newDate());
        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...newCheckUpgradeStatus().execute();
            // Set the last Launched date to today.
            SharedPreferences.Editoreditor= sharedPref.edit();
            editor.putString("LAST_LAUNCH_DATE", currentDate);
            editor.commit();
        }

Solution 3:

  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.

publicstaticStringgetCurrentDate() {
        DateFormat simpleDateFormat = newSimpleDateFormat("dd/MM/yyyy");
        Date date = newDate();
        return simpleDateFormat.format(date);
    }

2. SharedPreference class.

publicStringgetDatePreference() {
        SharedPreferences preferences = getPreference();
        return preferences.getString(DATE_KEY, null);
    }

    publicvoidsetDatePreference(String datePreference) {
        final SharedPreferences.Editor editor = getEditor();
        editor.putString(DATE_KEY, datePreference);
        editor.apply();
    }

3.Implement function showMessageOnceDay in Activity class.

publicshowMessageOnceDay(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());
        }
    }
    }

Solution 4:

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

Solution 5:

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

Post a Comment for "Show Alert On First Launch Application Of Each Day In Android"