[Java]
Import java. util. List;
Import java. util. Map;
Import android. content. Context;
Import android. content. SharedPreferences;
/**
* Tool for storing configuration information <br>
* Note: The data types that can be read include-<code> boolean, int, float, long, and String. </code>
*/
Public class SharePreferenceUtil {
Private final String MAK = "innoview ";
Private final int MODE = Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE;
Private final SharedPreferences sharedpreferences;
Public SharePreferenceUtil (Context context, String fileName ){
Sharedpreferences = context. getSharedPreferences (fileName, MODE );
}
Public boolean saveSharedPreferences (String key, String value ){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Try {
Editor. putString (key, AESEncryptor. encrypt (MAK, value ));
} Catch (Exception e ){
Editor. putString (key, value );
E. printStackTrace ();
}
Return editor. commit ();
}
Public String loadStringSharedPreference (String key ){
String str = null;
Try {
Str = sharedpreferences. getString (key, null );
If (str! = Null &&! "". Equals (str )){
Str = AESEncryptor. decrypt (MAK, str );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return str;
}
Public boolean saveSharedPreferences (String key, int value ){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Editor. putInt (key, value );
Return editor. commit ();
}
Public int loadIntSharedPreference (String key ){
Return sharedpreferences. getInt (key, 0 );
}
Public boolean saveSharedPreferences (String key, float value ){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Editor. putFloat (key, value );
Return editor. commit ();
}
Public float loadFloatSharedPreference (String key ){
Return sharedpreferences. getFloat (key, 0f );
}
Public boolean saveSharedPreferences (String key, Long value ){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Editor. putLong (key, value );
Return editor. commit ();
}
Public long loadLongSharedPreference (String key ){
Return sharedpreferences. getLong (key, 0l );
}
Public boolean saveSharedPreferences (String key, Boolean value ){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Editor. putBoolean (key, value );
Return editor. commit ();
}
Public boolean loadBooleanSharedPreference (String key ){
Return sharedpreferences. getBoolean (key, false );
}
Public boolean saveAllSharePreference (String keyName, List <?> List ){
Int size = list. size ();
If (size <1 ){
Return false;
}
SharedPreferences. Editor editor = sharedpreferences. edit ();
If (list. get (0) instanceof String ){
For (int I = 0; I <size; I ++ ){
Editor. putString (keyName + I, (String) list. get (I ));
}
} Else if (list. get (0) instanceof Long ){
For (int I = 0; I <size; I ++ ){
Editor. putLong (keyName + I, (Long) list. get (I ));
}
} Else if (list. get (0) instanceof Float ){
For (int I = 0; I <size; I ++ ){
Editor. putFloat (keyName + I, (Float) list. get (I ));
}
} Else if (list. get (0) instanceof Integer ){
For (int I = 0; I <size; I ++ ){
Editor. putLong (keyName + I, (Integer) list. get (I ));
}
} Else if (list. get (0) instanceof Boolean ){
For (int I = 0; I <size; I ++ ){
Editor. putBoolean (keyName + I, (Boolean) list. get (I ));
}
}
Return editor. commit ();
}
Public Map <String,?> LoadAllSharePreference (String key ){
Return sharedpreferences. getAll ();
}
Public boolean removeKey (String key ){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Editor. remove (key );
Return editor. commit ();
}
Public boolean removeAllKey (){
SharedPreferences. Editor editor = sharedpreferences. edit ();
Editor. clear ();
Return editor. commit ();
}
}