We usually save some data, will use the sharedpreferences, he is stored in the phone, the specific path is data/data/your package name/shared_prefs/Save the file name. XML, The use of sharedpreferences is also very simple, I wrote a Sharedpreferences tool class, and then saved here, and so on I need to save data directly from here Copy code, haha
The tool class is as follows
[Java]View Plaincopy
- Package com.example.shortcut;
- Import Android.content.Context;
- Import android.content.SharedPreferences;
- /**
- * Sharedpreferences of a tool class, call SetParam can save string, Integer, Boolean, Float, long type parameters
- * Also call GetParam to get the data stored in the phone
- * @author xiaanming
- *
- */
- Public class Sharedpreferencesutils {
- /**
- * File name stored in the phone
- */
- private static final String file_name = "Share_date";
- /**
- * Method of saving data, we need to get the specific type of the data to be saved, then call different save methods according to the type
- * @param context
- * @param key
- * @param Object
- */
- public static void SetParam (context context, String key, Object object) {
- String type = Object.getclass (). Getsimplename ();
- Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private);
- Sharedpreferences.editor Editor = Sp.edit ();
- if ("String". Equals (Type)) {
- Editor.putstring (Key, (String) object);
- }
- Else if ("Integer". Equals (Type)) {
- Editor.putint (Key, (Integer) object);
- }
- Else if ("Boolean". Equals (Type)) {
- Editor.putboolean (Key, (Boolean) object);
- }
- Else if ("Float". Equals (Type)) {
- Editor.putfloat (Key, (Float) object);
- }
- Else if ("Long". Equals (Type)) {
- Editor.putlong (Key, (Long) object);
- }
- Editor.commit ();
- }
- /**
- * Get the method to save the data, we are based on the default value to save the specific type of data, and then call the relative method to get the values
- * @param context
- * @param key
- * @param defaultobject
- * @return
- */
- public Static Object GetParam (context context, String key, Object defaultobject) {
- String type = Defaultobject.getclass (). Getsimplename ();
- Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private);
- if ("String". Equals (Type)) {
- return sp.getstring (Key, (String) defaultobject);
- }
- Else if ("Integer". Equals (Type)) {
- return Sp.getint (Key, (Integer) defaultobject);
- }
- Else if ("Boolean". Equals (Type)) {
- return Sp.getboolean (Key, (Boolean) defaultobject);
- }
- Else if ("Float". Equals (Type)) {
- return Sp.getfloat (Key, (Float) defaultobject);
- }
- Else if ("Long". Equals (Type)) {
- return Sp.getlong (Key, (Long) defaultobject);
- }
- return null;
- }
- }
It's also easy to use, save the data
[Java]View Plaincopy
- Sharedpreferencesutils.setparam (This, "String", "xiaanming");
- Sharedpreferencesutils.setparam (This, "int", 10);
- Sharedpreferencesutils.setparam (This, "Boolean", true);
- Sharedpreferencesutils.setparam (This, "Long", 100L);
- Sharedpreferencesutils.setparam (This, "float", 1.1f);
Get Data
[Java]View Plaincopy
- Sharedpreferencesutils.getparam (timeractivity. This, "String", " "); Sharedpreferencesutils.getparam (timeractivity. This, "int", 0);
- Sharedpreferencesutils.getparam (timeractivity. This, "Boolean", false);
- Sharedpreferencesutils.getparam (timeractivity. This, "long", 0L);
- Sharedpreferencesutils.getparam (timeractivity. This, "float", 0.0f);
is not very convenient, hope to read the article you have a little help!
Android shares a Sharedpreferences tool class for easy data saving