安卓第一行代码之SharedPreference

时间:2021-10-15 19:25:12

1.获取SharedPreference的方式:

context.getSharedPreference

activity.getPreference()

PreferenceManager.getDefaultSharedPreference()

2.数据存储

SharePreference.Editor editor = getSharedPreference("path",MODE_PRIVATE).edit();

 editor.putString("key1","value1");

 editor.putInt("key2",12);

    ······

editor.apply();


3.数据读取

SharePreference.Editor editor = getSharedPreference("path",MODE_PRIVATE).edit();

String receive1 = editor.getString("key1");

······