0

In my Android application for my school project I want to make it such that:

Every day, when you first open the application, it will start an activity. However, if you open the application again even after closing it from the multitasking view, it will not start the activity again.

I want it to be much like Elevate (https://www.elevateapp.com/) where on first startup it will say "Your training session for the day is ready" but never display this again if you open the app at another time in the day.

This is a screenshot of the activity: enter image description here

I have tried using AlarmManager in this link Alarm Manager Example and searching for answers but it did not work for me and I couldn't find any.

Is there a way to make it possible? Thanks in advance.

Community
  • 1
  • 1
meeps
  • 59
  • 1
  • 7

2 Answers2

3

We can use SharedPreferences to store the system date when the app is launched and verify if it is the same date or a different one every time the app s run.

If the date is different, store the new date into the SharedPreferences handle that you used.

To understand how to use SharedPreference to store data you can look at my answer here for an example.

Community
  • 1
  • 1
Prajnan Bhuyan
  • 415
  • 3
  • 11
1

First declare this two method on global level

   public static void commitPref(String key, String value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String readPref(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }

now maintain your logic..

 if (!readPref("CHECK_IF_RUN_TODAY", getApplicationContext()).equals(new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date()))){
            //YOUR LOGIC
            commitPref("CHECK_IF_RUN_TODAY", new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date()), getApplicationContext());
        }

but dont forget to upadte your prefrence after your logic Hope this helps..

Nikhil Borad
  • 2,065
  • 16
  • 20