For most beginners, if you want to use sharedpreferences for data storage, most people (including myself) should:
Store:
Sharedpreferences sharedpreferences = getsharedpreferences (context.getpackagename (), Context.MODE_PRIVATE); = Sharedpreferences.edit (); Editor.putstring ("username", "Tom"); Editor.putint ("password", 123456); Editor.commit ();
Get:
Sharedpreferences sharedpreferences = getsharedpreferences (context.getpackagename (), Context.MODE_PRIVATE); = Sharedpreferences.getstring ("username", ""); int password = sharedpreferences.getint ("password", 0);
Although this does not have a lot of code, if it is for beginners practice is harmless. But if you are doing a project development, it uses a large number of data storage. Each time the above code repeats over and over again, it is estimated to cause a person to crash. Therefore, for actual project development, it is best to encapsulate the package of the tool class that can be encapsulated.
The following is a packaged Sharedpreferences tool class, if you need to refer directly to OK:
PackageCom.example.utl;ImportJava.util.Map;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;/*** Save information Configuration class * *@authorAdmin*/ Public classSharedpreferenceshelper {Privatesharedpreferences sharedpreferences; /** Save the name of the phone*/ PublicString file_name = "Shared_data"; PrivateSharedpreferences.editor Editor; PublicSharedpreferenceshelper (Context context) {Sharedpreferences=context.getsharedpreferences (file_name, context.mode_private); Editor=Sharedpreferences.edit (); } /*** Storage*/ Public voidput (String key, Object object) {if(ObjectinstanceofString) {editor.putstring (key, (String) object); } Else if(ObjectinstanceofInteger) {Editor.putint (key, (Integer) object); } Else if(ObjectinstanceofBoolean) {Editor.putboolean (key, (Boolean) object); } Else if(ObjectinstanceofFloat) {Editor.putfloat (key, (Float) object); } Else if(ObjectinstanceofLong) {Editor.putlong (key, (Long) object); } Else{editor.putstring (key, object.tostring ()); } editor.commit (); } /*** Get saved data*/ PublicObject Getsharedpreference (String key, Object Defaultobject) {if(DefaultobjectinstanceofString) { returnsharedpreferences.getstring (Key, (String) defaultobject); } Else if(DefaultobjectinstanceofInteger) { returnSharedpreferences.getint (Key, (Integer) defaultobject); } Else if(DefaultobjectinstanceofBoolean) { returnSharedpreferences.getboolean (Key, (Boolean) defaultobject); } Else if(DefaultobjectinstanceofFloat) { returnsharedpreferences.getfloat (Key, (Float) defaultobject); } Else if(DefaultobjectinstanceofLong) { returnSharedpreferences.getlong (Key, (Long) defaultobject); } Else { returnSharedpreferences.getstring (Key,NULL); } } /*** Remove a value that already corresponds to a key value*/ Public voidRemove (String key) {editor.remove (key); Editor.commit (); } /*** Clear All data*/ Public voidClear () {editor.clear (); Editor.commit (); } /*** Query whether a key exists*/ PublicBoolean contain (String key) {returnsharedpreferences.contains (key); } /*** Returns all key-value pairs*/ PublicMap<string,?>GetAll () {returnSharedpreferences.getall (); }}
Android-Developed Sharedpreferences package