I am using shared preferences to store userId once user login to the app. I need to expire(delete) the stored userId after 30 days once the userId is stored on shared preferences.
Is it possible to get the last modified date and time of specific shared preference value.
It's not possible, SharedPreferences don't have any built in method to do this. You'll have to store last modification date manually, possible in SharedPreferences as well.
I would say you can get the path of the SharedPreferences file and try using lastModified() of File class like,
File file = new File("path of shared-preference file");
file.lastModified();
Actually you could create an Alarm: you can set it to run within 30 days from the "registration" for the value stored in SharedPreferences.
You can then use a Receiver (search for more examples) to catch the alarm and run your code.
An example here
Related
I have a string and a button as follows:
String message = "Hello World"
button.OnClickListener(......){
message = "123456789";
}
So here is what I want,
When the app starts the string is "Hello World" but when the user clicks the button it changes to "123456789" and I want the string to change permanently for lifetime.
So when the user restarts the application or reinstall it the string is still
"123456789".I think this comes under Shared Preferences.
Please Help,I really need this
You can save that string in Shared Preferences, and get it all the from there. If is no value, you can get default value from resources or you can provide your own default String. Note that you can do that for lifetime. If user delete the app or clear cache, your view will display default value, Hello World.
Edit
You can use android:allowBackup="true" from manifest in order to keep old Shared Preference values.
There is a chance data from SharedPreferences is gone when you reinstall especially if you uninstall the app first, same case as local database using SQLite. You can try using database from your local server.
To save a String in SharedPreferences :
SharedPreferences.Editor editor = getSharedPreferences("com.package.name", Context.MODE_PRIVATE).edit();
editor.putString("keyName", message);
editor.apply();
To get it back :
SharedPreferences sharedPrefs = getSharedPreferences("com.package.name", Context.MODE_PRIVATE);
message = sharedPrefs.getString("keyName",defaultValue);
When the user modifies it, save it. When the app start, get it with as default value "Hello World" so that while the user hadn't click the button, it will stay as Hello World.
For Restarting Shared Preference is the best mechanism but for reinstall allowbackup works on API level 23 and above,
you can consider to update the value on server once you update in your Shared Preferences for the first time and during reinstall can do a API call check for the same for the particular user.
I am loading getting data from a csv file then inserting them into SQLite at the launch of my app but it does this every time I open my app. Is there a way that that the app will only iterate through the CSV only on the first use of the app so that the succeeding launches will be quicker?
Thank you in advance :)
You can use SharedPreferences to determine if you already parsed your csv file.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("someKey", "1");
editor.commit();
And before you run your task you should check if you had manipulated your csv already by checking if the "someKey" exsit.
There are several ways that you can avoid to call for CSV file at each time.
You can save a small data (Saving Key-Value Sets) on the app setting using SharedPrefernce.
You can save a small int value on shared preference that indicates you have already called the CSV file or not.
At the beginning you have to call for the CSV load function with in a if conduction that checks whether the sharedpreference value has 1 or 0
if the shared pererence value has 0 the u will enter to the function and insert to the database. after that with in the if condition you will change the shared preference value to 1..
so the next time when you open the app, the shared preference value will be 1... if condition will not be satisfied.. the function will not be called...
Hope u understand
How do I save an integer that can be overwritten and then read it on android? I am using libgdx. I know how to do it for my desktop, but I want internal storage.
You should use libgdx Preferences.
That way it will work cross-plattform! (including Android)
Since you just want to store some Integer that should be just what you need.
See example:
//get a preferences instance
Preferences prefs = Gdx.app.getPreferences("My Preferences");
//put some Integer
prefs.putInteger("score", 99);
//persist preferences
prefs.flush();
//get Integer from preferences, 0 is the default value.
prefs.getInteger("score", 0);
There are different ways to do that:
You can write to a FileHandle. Just create a FileHandle like Gdx.files.local("myfile.txt"); and write to it using fileHandle.writeString(Integer.toString(myInt));
Note, that Internal and Classpath are read-only, so you can't write files there.
Also note, that not all types of the gdx.files. are usable for all backends. More on that here.
The second way to do that are the Preferences. The Preferences are the only way to have persistent data for HTML5 applications.
The Preferences are XML files in which you can store, read and change data.
To create Preferences for your app you just need to call:
Preferences myPref = Gdx.app.getPreferences("PreferenceName");
Note, that you should use the full name, for example com.stackoverflow.preferences, as on desktop all Preferences use the same file.
To write data you can use myPref.putInteger("name", value) for Integers, myPref.putBoolean(("name", value) for Booleans... Make sure you call myPref.flush() after all changes, to save the new data.
To get the data you can call myPref.getInteger("name, defaultValue"). The default value is returned if there is no data with the given name.
More on Preferences here.
Hope i could help.
I Am writing a code using SharedPreference to store username and password of a user but each time I entered information the older one in xml file are override by newer one what I have to to to get all my data?
SharedPreferences sp1=getSharedPreferences("myshared", 0);
sp1.edit().putString("name", name.getText().toString()).commit();
sp1.edit().putString("pass", pass.getText().toString()).commit();
sp1.edit().putString("age",age.getText().toString()).commit();
sp1.edit().putString("id",id.getText().toString()).commit();
It sounds like you're overwriting your shared preferences between activities. SharedPreferences are persistent like a file on disk, so you shouldn't ever have an issue with the values not being set, hence why you must be overwriting it.
You can get your SharedPreferences by doing
SharedPreferences sp1=getSharedPreferences("myshared", 0);
String name = sp1.getString("name", "noname");
String pass = sp1.getString("pass", "nopass");
...
You can determine if the name/pass was set by checking if they equal the default value (noname and nopass in this case, though it could easily be null).
I am developing a android application where I need time stamp should be saved in time picker even if we click back button in android.Since I am new to android I need help.Thanks in advance for help.
System.currentTimeMillis();
Will get you the current timestamp.If you want to save it somehow you may use SharedPrefences or sqlite db or something like this.
I think I would prefer SharedPreferences since you want the timestamp to be there after clicking the back button. Clicking that button might exit you application so some persistent storage would be fine. The timestampt is no POJO so SharePreferences are my first choice.
Something like that should help:
// That will save the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.edit().putLong("PREF_TAG_TIMESTMAP", System.currentTimeMillis()).commit();
pref.edit().clear().commit();
// That will get you the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.getLong("PREF_TAG_TIMESTMAP", -1.0);
If you need to persist you data for a later run of the activity you can do that using the sql database, shared preferences or if you only need to persist during the current run of the app (not for subsequent launched of the app) you could create a static variable holding the values you want to store for you.
From what I understand you should go with the static variable approach.