Android中SharedPreferences的用法(一)
2015-08-02 23:48
579 查看
有些时候,应用程序有少量的数据需要保存,而且这些数据的格式很简单:都是普通的字符串、标量类型的值等(基本数据类型),比如应用程序的各种配置信息(是否打开音效、是否使用振动)、小游戏的玩家积分(扫雷)等。对于这种数据,Android提供了SharedPreferences进行保存。SharedPreferences数据可供本应用程序使用,也可供其他应用程序共享,此处先介绍供本程序使用的方法。
SharedPreferences使用步骤
1、获取SharedPreferences对象:
name:SharedPreferences文件的名字
mode:有三种模式(1)
2、读出数据:使用
其他方法:
3、写入数据:
(1)获取SharedPreferences内部接口Editor:
(2)
(3)
其他方法:
以下为一个读写SharedPreferences数据的小示例,项目名为DemoSharedPreferences
activity_main.xml布局文件
com.example.demosharedpreferences.MainActivity.java代码
SharedPreferences使用步骤
1、获取SharedPreferences对象:
getSharedPreferences(String name,int mode),该方法由Context提供。
name:SharedPreferences文件的名字
mode:有三种模式(1)
Context.MODE_PRIVATE(SharedPreferences数据只能被本应用程序读写);(2)
Context.MODE_WORLD_READABLE(能被其他应用程序读);(3)
Context.MODE_WORLD_WRITEABLE(可以被其他应用程序读写)
2、读出数据:使用
xxx getXxx(String key,xxx default)方法获取指定key对应的value,如果不存在则返回默认值。其中xxx可以是boolean、float、int、long、String等各种基本类型。
其他方法:
boolean contains(String key):判断SharedPreferences是否包含特定key的数据。
abstract Map<String,?> getAll():获取所有的key-value对。SharedPreferences的根元素是
<map.../>
3、写入数据:
(1)获取SharedPreferences内部接口Editor:
SharedPreferences.Editor editor = (SharedPreferences对象).edit();
(2)
SharedPreferences.Editor.put(String key,xxx value):存入指定key对应的数据,其中xxx可以是boolean、float、int、long、String等各种基本类型。
(3)
SharedPreferences.Editor.commit():提交Editor的所有修改
其他方法:
SharedPreferences.Editor.clear():清空所有数据
SharedPreferences.Editor.remove(String key):删除指定key对应的数据
以下为一个读写SharedPreferences数据的小示例,项目名为DemoSharedPreferences
activity_main.xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_marginBottom="10dip" android:gravity="center" android:text="SharedPreferences读写本应用数据测试" /> <Button android:id="@+id/bt_write" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="写入数据" /> <Button android:id="@+id/bt_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取数据" /> </LinearLayout>
com.example.demosharedpreferences.MainActivity.java代码
package com.example.demosharedpreferences; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button bt_write,bt_read; private SharedPreferences sp; private SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_write = (Button) findViewById(R.id.bt_write); bt_read = (Button) findViewById(R.id.bt_read); // 获取SharedPreferences对象,文件名为preferences,模式为仅供本应用程序使用 sp = getSharedPreferences("preferences", MODE_PRIVATE); // 获取SharedPreferences.Editor对象 editor = sp.edit(); bt_write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss"); // 存入当前时间 editor.putString("time", sdf.format(new Date())); // 存入一个随机数 editor.putInt("random", (int)(Math.random()*100)); // 提交所有存入的数据 editor.commit(); } }); bt_read.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 读取字符串数据 String time = sp.getString("time", null); // 读取int类型数据 int randNum = sp.getInt("random", 0); String result = time==null?"您暂时还未写入数据" :"写入时间:" + time + "\n上次生成的随机数为:" + randNum; Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); } }); } }
相关文章推荐
- Android LeakCanary 内存检测Eclipes版本
- 在ubuntu14.04上编译android4.2.2系统
- Android中收货地址管理Demo
- Android中数据存储之一------SharedPreferences
- service android 属性
- android重力感应开发
- Android开发之路--(2)--Android四大组件
- BLE开发的各种坑
- 关于android软键盘隐藏总结
- 像素、分辨率、屏幕大小(屏幕适配必看)
- Android 评分组件RatingBar、ImageView图片、 ImageButton图片按钮的综合运用
- android上传图片到PHP后台全过程
- android夜间模式实现方式
- Android创建数据库的时候出现Not Table错误解析
- Android初级第三次小结
- Android初级第二次小结
- Android初级第一次小结
- Android输入法的关闭打开
- Android基础知识之四大组件Activity(四)保存Activity的状态
- Android Api Demos登顶之路(十五)Receive Result