Piglet's Android starter Road Day 7 Part 2

Source: Internet
Author: User

Piglet's Android starter Road Day 7 Part 2

--sharedpreferences of Android data storage and access (save preferences)

---reprint please specify the Source: Coder-pig

Introduction to this section:

In Part 1 we learned to read and write files in Android, and in Part 2 we introduced a lightweight storage class--

Sharedpreferences (Preferences Save) is used to save some user's settings in the software, such as a game app, the user

You can set whether to open the game sound, whether automatic login, etc.! The next time you start the app, you don't have to set it up again! We all know that.

Software under Windows systems typically uses INI files as a configuration file, while using the Properties property file in J2SE with XML

File to save the configuration information of the software, and in Android we often use Sharedpreferences to save the software configuration, of course

Behind his back is the use of XML files to store data! OK, let's start Part 2!


Learning Roadmap for this section:




Body:


sharepreferences Introduction and use of the process:



code example:

:

The process is to enter the account password and click Login, save the information to the Sharedpreference file,

Then restart the app and see that the data is already displayed in the text box.



Open the file Expoler open the data/data/< package name at the same time > You can see that an XML file was generated in the Shared_prefs directory



Click Export to desktop to see what's Inside:



Reference code Download: code Download







Read and write sharedpreferences for other applications



code example:

:

is to read the sharedpreferences stored in the previous demo and show it through a toast.



The code is very simple, just one activity, here to paste it!

Package Com.example.sharedpreferencesdemo2;import Android.app.activity;import Android.content.context;import Android.content.sharedpreferences;import Android.content.pm.packagemanager.namenotfoundexception;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.toast;public class Mainactivity extends Activity {private Button btnshow;private Context Othercontext;private sharedpreferences sp; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btnshow = (Button) findViewById ( R.id.btnshow); Btnshow.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Gets the package name of the first app, To obtain the corresponding context, you need to capture the exception in try {Othercontext = Createpackagecontext ("Com.jay.example.sharedpreferencedemo1", context.context_ignore_security);} catch (Namenotfoundexception e) {e.printstacktrace ();} The corresponding SHAREDPREFERENCESSP is obtained according to the context = OthercoNtext.getsharedpreferences ("MYSP", context.mode_world_readable); String name = sp.getstring ("username", ""); String passwd = sp.getstring ("passwd", "" "); Toast.maketext (Getapplicationcontext (), "Demo1 's sharedpreference saved user name:" + name + "\ n Password =" + passwd, Toast.length_short ). Show ();}});}}


PS: Well, about the use of sharedpreferences here, in fact, for the XML file, we can also do their own parsing, such as Android

Special Xmlpull parser, or use dom,sax and so on in the follow-up will be specifically summarized under the XML file parsing, please look forward to, and thank you all friends

Support! Blog for a period of time a lot of feelings, I am not what Daniel, not what big on the technology, I just silently do some insignificant

Things, I just want to write a clear point of the article, for want to learn programming and feel confused friends a direction, not to be impossible, now on the market related

The reference book is a mixed bag, to some knowledge speak foggy, finish learning as if did not learn the same! My personal thinking is to master the basic theoretical basis, know how

Use it! After learning the foundation, more important is to practice, through the project to accumulate code, consolidate knowledge points, start with small projects, and finally do a

Big project! The purpose of this series is to get started, and then the second series is to apply these basics through the actual small project, so please wait!

Once again thank you friends for their support, I will insist on writing down! As long as someone wants to watch!









Span style= "font-family: ' Microsoft yahei '" >

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.