Simple game data storage and simple game Data Storage
In game development, you sometimes need to set up persistent and simple data storage.
Preferences is mainly simple to use, but not very powerful. It is generally suitable for saving some simple parameters set by users and is a lightweight storage mechanism. Preferences can only be used to store several simple types of data, such as boolean, int, floate, long, or String. The data is stored in the xml file in the Preferences directory of the application as a key-value pair.
You can use SharedPreferences sp = this. getSharedPreferences ("feiruo", Context. MODE_PRIVATE );
Or SharedPreferences sp = PreferencesManager. getdefasharsharedpreferences (Context );
Package com. mycompany. myapp; import android. app. *; import android. OS. *; import android. view. *; import android. widget. *; import android. content. *; import java. util. *; public class MainActivity extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the SharedPreferences reference and store it as feiruo. xml. The read/write mode is private.
// SharedPreferences sp = PreferencesManager. getdefasharsharedpreferences (this );
SharedPreferences sp = this. getSharedPreferences ("feiruo", Context. MODE_PRIVATE); String lasttime = sp. getString ("time", null); if (lasttime = null) {lasttime = "Hello, welcome to visit";} else {lasttime = "Hello, Last login time is: "+ lasttime;} SharedPreferences. editor ed = sp. edit (); ed. putString ("time", new Date (). toLocaleString (); ed. commit (); // submit the modification; TextView TV = (TextView) this. findViewById (R. id. mainTextView); TV. setText (lasttime );}}