[Tool] sharedpreferences encryption for Android Data Storage

Source: Internet
Author: User

[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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.