In-game simple data storage

Source: Internet
Author: User

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

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.