Baidu on a lot about the single-instance mode of interpretation, such as lazy mode, a hungry man mode, and so on, also said that the singleton mode with Sharedpreferences, because Sharedpreferences temporarily does not support multi-threaded access. You can view this article in my Android sharedpreferences lightweight storage described in Sharedpreferences.
Paste the code below to see the singleton pattern used with sharedpreferences.
Import Android.content.context;import Android.content.contextwrapper;import android.content.SharedPreferences; Import Android.content.sharedpreferences.editor;public class Programconfigmanage {//Singleton mode class object Private Contextwrapper Mwrapper = null;private static Programconfigmanage m_programconfig = null;private sharedpreferences mPreferences = null;/ /define the configuration parameter as a member global variable private string mstrnetworkip;private int mnnetworkport;private string mstrnetworkusername; Private String Mstrnetworkpassword; Private String mstrnetworkupdate; Singleton mode gets an instance of public static Programconfigmanage getinstance (Context base) {if (m_programconfig = = null) {M_programconfig = NE W Programconfigmanage (base);//To increase the speed, read the configuration here M_programconfig.mstrnetworkip = m_ ProgramConfig.mPreferences.getString ("Config_networkip", ""); M_programconfig.mnnetworkport = m_ ProgramConfig.mPreferences.getInt ("Config_networkport", 0); m_programconfig.mstrnetworkusername = m_ ProgramConfig.mPreferences.getString ("Config_networkusername", "" "); m_programconfiG.mstrnetworkpassword = m_ProgramConfig.mPreferences.getString ("Config_networkpassword", ""); m_ Programconfig.mstrnetworkupdate = m_ProgramConfig.mPreferences.getString ("Config_networkupdate", "");} return m_programconfig;} Configure Getsharedpreferencespublic Programconfigmanage (Context base) {//super (base); mwrapper = new Contextwrapper (base); Mpreferences = mwrapper.getsharedpreferences ("Perference", Contextwrapper.mode_private);} Set public void Setnetworkstate (string strnetworkip, int nnetworkport,string strnetworkusername, string Strnetworkpassword) {Editor editor = Mpreferences.edit (); Editor.putstring ("Config_networkip", Strnetworkip); Editor.putint ("Config_networkport", Nnetworkport); Editor.putstring ("Config_networkusername", StrNetworkUserName) ; Editor.putstring ("Config_networkpassword", Strnetworkpassword); Editor.commit (); mstrnetworkip = StrNetworkIP; Mnnetworkport = Nnetworkport;mstrnetworkusername = Strnetworkusername;mstrnetworkpassword = StrNetworkPassword;} Set public void SetnetworkuPdatemessage (String strupdate) {Editor editor = Mpreferences.edit (); Editor.putstring ("Config_networkupdate", Strupdate); Editor.commit (); mstrnetworkupdate=strupdate;} Public String Getnetworkip () {return mstrnetworkip;} public int Getnetworkport () {return mnnetworkport;} Public String Getnetworkusername () {return mstrnetworkusername;} Public String Getnetwordpassword () {return mstrnetworkpassword;} Public String Getnetworkupdate () {return mstrnetworkupdate;}}
You are welcome to exchange more sharedpreferences and the use of singleton patterns.
Android Singleton mode for use with sharedpreferences