Day 7 part 2, androidpart
Day 7 part 2, the road to getting started with Piggy's Android
Data storage and access in Android -- SharedPreferences (save preferences)
--- Reprinted with the source: coder-pig
This section introduces:
In Part 1, we learned how to read and write files in Android, and in Part 2, we will introduce a lightweight storage class --
SharedPreferences (preference parameter saving) is used to save some user settings in the software, such as a game app, user
You can set whether to enable the game sound and whether to log on automatically! The next time you start the app, you do not need to set it again! We all know
In Windows, the software usually uses the INI file as the configuration file, and the properties property file and xml file are used in J2SE.
File to save software configuration information. In Android, we often use SharedPreferences to save software configuration. Of course
It uses xml files to store data! Now, let's start learning part 2!
This section describes the road map:
Body:
Introduction and use of SharePreferences:
Sample Code:
:
The process is to enter the account password and click Login to save the information to the SharedPreference file,
Restart the app and check that the data is displayed in the text box.
Open File Expoler and open data/<package name>. You can see that an xml File is generated under the shared_prefs directory.
Click Export to the desktop to view the content:
Download reference code:Code download
Read and Write SharedPreferences of other applications
Sample Code:
:
Read the SharedPreferences stored in the previous Demo, and display them through Toast.
The code is very simple. It's just an Activity. paste it here!
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) {// obtain the package name of the first application to obtain the corresponding Context, capture exceptions. try {othercontext = createPackageContext ("com. jay. example. sharedpreferencedemo1 ", Context. CONTEXT_IGNORE_SECURITY);} catch (NameNotFoundException e) {e. printStackTrace ();} // obtain the corresponding SharedPreferencessp = othercontext according to the Context. getSharedPreferences ("mysp", Context. MODE_WORLD_READABLE); String name = sp. getString ("username", ""); String passwd = sp. getString ("passwd", ""); Toast. makeText (getApplicationContext (), "The SharedPreference username of Demo1 is:" + name + "\ n password is" + passwd, Toast. LENGTH_SHORT ). show ();}});}}
Ps: Well, the usage of SharedPreferences is mentioned here. In fact, we can parse xml files by ourselves, such as Android
The unique XMLpull parser or DOM and SAX will be used to summarize the xml file parsing in the future. Thank you for your attention.
Yes!I have been feeling a lot during my time writing. I am not a great guy, and I don't have any technical skills. I just silently do something insignificant.
I just want my own articles to be clear and give a direction to my friends who want to learn programming and are confused.
The reference books are mixed, and I am confused about some knowledge. I didn't seem to have learned it! My personal idea is to grasp the basic theoretical basis first.
Why! After learning the basics, the more important thing is to use projects to accumulate coding volumes, consolidate knowledge points, start with a small project, and then create
Big project! The purpose of this series is to get started, and the second series is to apply these basic knowledge through small projects. Please stay tuned!
Once again, I would like to thank all of you for your support! As long as someone is willing to watch it!
Where can I download the me2day mobile phone software? Android
You just need to download it by phone.
Me2day Android, Chinese version, 1039995917 @ qqcom
Sent