Android, the data saved, described earlier, save the data to memory and SD card, this time we will introduce, more commonly use sharedpreferences way to save data,
1, get the object of Sharedpreferences,
2, using sharedpreferences to get the editor, is edit (essentially a map)
3, put the data inside, put out, to remember when a transaction, remember to commit commit.
public static void Saveuserinfo (Context context,string username,string password) {Sharedpreferences sp = Context.getsharedpreferences ("config", context.mode_private);//Get the Editor editor of SP edit = Sp.edit (); Edit.putstring (" Username ", username); edit.putstring (" Password ", password);//similar to the database of things, to ensure that the data submitted successfully edit.commit ();
Data saved well, how to read out the data, in fact, just reported that the data, is an XML file, its location is/data/data/package name/shared_prefs (Reborn)/, here is the path of the program to generate XML,
Read the time, is the first to get Sharedpreferences object, and then, through key can get the corresponding hungry value, more convenient.
Sharedpreferences sp = getsharedpreferences ("config", context.mode_private); String username = sp.getstring ("username", "" "); String Password = sp.getstring ("Password", "" "); Et_username.settext (username); et_password.settext (password);
Note: This article to undertake the previous article: http://www.cnblogs.com/fengtengfei/p/3961089.html, contact together to see, the idea is more clear.
Android Learning notes-How to save data 2-sharedpreferences