Sharedpreferences are often used to store small data, such as logging the user name and password locally. Of course, its data is stored locally. If the application data is cleared, the stored data here is gone.
I have done this before to determine whether the user is the first to enter the software after installation. This is used for data storage. This is another requirement in the projects I have made during this period, A tool class for sharedpreferences storage is provided below.
public class Editer { Context ct; SharedPreferences preferences; public Editer(Context ct) { this.ct=ct; } public Boolean saveinfo(String name, String key,String value) { preferences = ct.getSharedPreferences(name,1); Editor editor = preferences.edit(); editor.putString(key, value); Boolean bres= editor.commit(); return bres; } public String getinfo(String name,String key) { String res=""; preferences = ct.getSharedPreferences(name,1); res=preferences.getString(key, ""); return res; }}
Call in activity:
Editer ed = new Editer(MainActivity.this);
Then, call the corresponding storage and read the method.
Storage Method
Ed. saveinfo ("iport", "ip", ip); iport indicates the tag value, IP indicates the tag value, and IP indicates the value you want to store.
Obtain Method
ed.getinfo("iport", "ip")
Here, the stored IP value is taken out.
Delete Method
localOrder.prefs.edit().clear().commit();