SharedPreferences一个轻量级的存储类,特别适合用于保存软件配置参数
它用xml文件存放数据,文件存放在/data/data/<packagename>/shared_prefs 目录下
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); savetoSharedPreferences(); readfromSharedPreferences(); } private void readfromSharedPreferences() { //1.打开名为account的配置文件 SharedPreferences sharedPreferences = getSharedPreferences("infomation", 0); //2.获取数据 boolean autostart = sharedPreferences.getBoolean("autostart", true); float autoclose = sharedPreferences.getFloat("autoclose", 0); String nickname = sharedPreferences.getString("nickname", ""); //看看有没有拿到数据 Toast.makeText(this, nickname+","+autostart+","+autoclose, 1).show(); } private void savetoSharedPreferences() { //获取sharedPreferences SharedPreferences sp =getSharedPreferences("infomation",MODE_PRIVATE); //获取editor对象 Editor editor =sp.edit(); //使用editor进行写你需要的数据到sp里面 editor.putBoolean("autostart", false); editor.putFloat("autoclose", 1000); editor.putString("nickname", "hellokitty"); //将我们写入的数据保存到文件中 editor.commit(); } }