Esperandro Introduction
The Esperandro Library helps developers to use sharedpreferences by identifying existing key-value pairs and corresponding types. And for every preferences data that uses the Sharedpreference naming method, it's both safe and easy. Esperandro uses a comment processor to generate a class at compile time to define an interface. This class is used when perferences data is stored and accessed later.
Official website http://dkunzler.github.io/esperandro/
Add a jar package or dependency
There's a lot of trouble in Eclipse, just like butterknife. Reference http://blog.csdn.net/chenyufeng1991/article/details/46900395
The name in the new interface inheritance Sharedpreferenceactions @SharedPreferences is the file name in the SP folder
1@SharedPreferences (name= "setting", mode=sharedpreferencemode.private)2 Public InterfaceSettingspextendssharedpreferenceactions {3 4 intThemevalue ();5 voidThemevalue (intthemevalue);6 7 String strvalue ();8 voidstrvalue (String strvalue);9 Ten Booleanboolvalue (); One voidBoolvalue (Booleanboolvalue); A -}
Set the value that needs to be deposited, support int, long, float, Boolean, String and Set<string>
The default value is
int --1long - -1lfloat - -1.0fbooleanfalse-"" (Empty String ) Setnull
If you want to set the default value above add @default to set it in front of the type to configure
@Default (ofint=1) int themevalue (); void themevalue (int themevalue); @Default (ofstring= "hehe") String strvalue (); void strvalue (String strvalue); @Default (Ofboolean=true) boolean boolvalue (); void Boolvalue (boolean boolvalue);
Get instance Settingsp.class is the interface that inherits Sharedpreferenceactions
Esperandro.getpreferences (class<t> Preferenceclass, Context context)
SETTINGSP settingsp= esperandro.getpreferences (settingsp. Class, Mcontext);
With an instance, you can get and set the value so that the directly obtained value is the default value at the beginning, and the Data/data file SP folder is not generated.
String str=settingsp.strvalue (); int themevalue=settingsp.themevalue (); boolean bool=settingsp.boolvalue ();
The setting file in the SP folder is generated when the value is set.
Settingsp.strvalue ("haha"); Settingsp.themevalue (666);
There may be more than one such interface in the actual situation and it can be cumbersome to generate instances of each activity within the baseactivity.
1 //sharedpreferences2 protected<P> P getsharedpreferences (class<p>Spclass) {3 returnEsperandro.getpreferences (Spclass, This);4 }5 6 Publicaccountsharedpreferences getaccountsharedpreferences () {7 returnGetsharedpreferences (accountsharedpreferences.class);8 }9 Publicsettingssharedpreferences getsettingssharedpreferences () {Ten returnGetsharedpreferences (settingssharedpreferences.class); One } A Publiclocationsharedpreferences getlocationsharedpreferences () { - returnGetsharedpreferences (locationsharedpreferences.class); -}
Then, in the activity, what interface will be called directly to the following 3 the interface of the Get method is OK to get the value and set the value of the method as follows
String str=getsettingssharedpreferences (). strvalue (); Getsettingssharedpreferences (). Themevalue (666);
Use of Esperandro