-1

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() ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

0

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

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();
        }
Denny Mathew
  • 842
  • 3
  • 13
  • 31
-1

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

    }
}
Anthony Cannon
  • 1,245
  • 9
  • 20
  • 1
    I'm confused. getSharedPreferences returns a SharedPreference object. Why should the filename of the Shared Preference (your proposed KEY_DAY constant) be the same as the lastday saved value (what you have in getInt) – Zun Jun 20 '18 at 09:19
  • This answer is much better: https://stackoverflow.com/a/50944621/3999808 – Zun Jun 20 '18 at 09:21
  • Sorry, I miss understood the question. I have updated my answer. – Anthony Cannon Jun 20 '18 at 09:24
  • @ZUNJAE you're confused because this code is confusing. Probably following [MDD](http://codemanship.co.uk/parlezuml/blog/?postid=147) principles... – 2Dee Jun 20 '18 at 09:24