Using SharedPreferences to get the day of the month

116 Views Asked by At

I want to run an activity just once in a day. I found this way to do so:

    Calendar calendar = Calendar.getInstance();
    int currentday = calendar.get(Calendar.DAY_OF_MONTH);
    Log.d("Today",""+currentday);

    SharedPreferences settings = getSharedPreferences("DAY", 0);
    int lastday = settings.getInt("day", 0);
    Log.d("Last day",""+lastday);

 if(lastday==currentday){Toast.makeText(MainActivity.this,"Activity will run just once a day",Toast.LENGTH_SHORT).show();}
}

Say the value of currentday is 20, How does lastday get it's value from settings.getInt() ?

3

There are 3 best solutions below

4
Anthony Cannon On

Here is an example in Java on how to do this:

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainActivity";

    public static final String PREF = "PREF";
    public static final String KEY_DAY = "KEY_DAY";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences pref = getSharedPreferences(PREF, Context.MODE_PRIVATE);

        // Set
        pref.edit().putInt(KEY_DAY, 20).apply();

        // Get
        int lastDay = pref.getInt(KEY_DAY, 0);
        Log.d(TAG, "lastDay: " + lastDay);

    }
}
0
AudioBubble On

Try this way first store date into shared preferesce.

 SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("day", 12);
 editor.apply();

and getting data like below ..

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int day = prefs.getInt("day", 0); //0 is the default value.
0
Denny Mathew On

Here is something which i had used in my app for storing and fecthing within seperate class called AppPreferences. Probably you can modify and use it, also log the values incase you want to check values are fetched and stored. Let me know, if i answered you

public static final String PREFS_KEY = "example_key";  
public static final String APP_PREFS = "example_prefs";


     /**
         * Returns the value stored
         *
         * @return String containing the value
         */
        public static String getValueStored() {
            SharedPreferences prefs = AppAplication.getContext().getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
            return prefs.getString(PREFS_KEY , "");
        }

     /**
         * Sets the value
         *
         * @return true if the value was saved, false on failure
         */
        public static boolean setValueToStore(final String url) {
            SharedPreferences prefs = AppAplication.getContext().getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
            SharedPreferences.Editor prefsEditor = prefs.edit();
            prefsEditor.putString(PREFS_KEY , url);
            return prefsEditor.commit();
        }