Android阶段学习笔记 16.8.6 之 SharedPreferences存数据

来源:互联网 发布:在淘宝开店快递怎么弄 编辑:程序博客网 时间:2024/06/05 06:30

在手机安全卫士的项目中,使用了多次SharedPreferences

1.先定义一个SharedPreferences对象,用于整个项目数据的存储

SharedPreferences mPref= getSharedPreferences("config",Activity.MODE_PRIVATE);

2.存数据并保存

SharedPreferences.Editor editor = mPref.edit();

1)editor.putBoolean("auto_Update", true);

2)editor.putString("status", "happy");

3)editor.putInt("age", 3);

editor.commit();

或者用一句

mPref.edit().putBoolean("auto_Update", true).commit();

在File Explorer的data/data/相应的包名/shared_prefs/config.xml 下导出该文件可看到自己存储的信息

3.拿数据,默认为true

boolean isUpdate = mPref.getBoolean("auto_Update", true);
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map>        <string name="status">happy</string><pre name="code" class="html">    <boolean name="configed" value="true" />
 <int name="age" value="3" /> </map>


0 0
原创粉丝点击