Simple game data storage and simple game Data Storage

Source: Internet
Author: User

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 );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.