Use SharePreferences to access data (MOOC notes), sharepreferences
0. video address: http://www.imooc.com/video/3265
1. Use SharePreferences to access data:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); SharedPreferences pref = PreferenceManager. getdefasharsharedpreferences (MainActivity. this); // Method 2: SharedPreferences // pref = getSharedPreferences ("mypref", MODE_PRIVATE); Editor editor = pref. edit (); editor. putString ("name", ""); editor. putBoolean ("default", true); editor. putInt ("age", 30); editor. putLong ("time", System. currentTimeMillis (); editor. commit (); // submit // System. out. println (pref. getInt ("age", 0 ));}}
View Code
2. How to set the value?
3. How to view data files? 1) run the program, and then open DDMS> File Explorer> data> package name> shared_prefs> pref. xml> Export 2) open the File: