使用SharePreferences存取数据(慕课笔记 )

0.视频地址:http://www.imooc.com/video/3265

1.使用SharePreferences存取数据:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        //方法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();//提交
        //System.out.println(pref.getInt("age", 0));
    }
}
View Code

2.如何取值?

3.如何查看数据文件?
1)运行程序,然后打开DDMS>File Explorer>data>data>包名>shared_prefs>pref.xml>导出
2)打开文件:
 
 
原文地址:https://www.cnblogs.com/moonlightml/p/6363961.html