In game development, there are times when you want to set up a persistent simple data store.
Preferences is mostly simple to use, but the function is not very powerful, generally suitable for saving some simple user-set parameters, is a lightweight storage mechanism. Preferences can only be used to store data of several simple types, such as: boolean, int, floate, long, or string. This data is stored as a key-value pair in an XML file in the application's private preferences directory.
Can be used sharedpreferences sp=this.getsharedpreferences ("Feiruo", context.mode_private);
or sharedpreferences sp=preferencesmanager.getdefaultsharedpreferences (Context);
PackageCom.mycompany.myapp;Importandroid.app.*;Importandroid.os.*;Importandroid.view.*;Importandroid.widget.*;Importandroid.content.*;ImportJava.util.*; Public classMainactivityextendsactivity{/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //get sharedpreferences Reference, store name Feiruo.xml, read-write mode private
//sharedpreferences sp=preferencesmanager.getdefaultsharedpreferences (this);
Sharedpreferences sp= This. Getsharedpreferences ("Feiruo", context.mode_private); String Lasttime=sp.getstring ("Time",NULL); if(lasttime==NULL) {Lasttime= "Hello, welcome to the first visit"; }Else{lasttime= "Hello, last Login time:" +Lasttime; } sharedpreferences.editor Ed=Sp.edit (); Ed.putstring ("Time",NewDate (). toLocaleString ()); Ed.commit ();//submit changes;TextView tv= (TextView) This. Findviewbyid (R.id.maintextview); Tv.settext (Lasttime); }}
In-game simple data storage