SharedPreferences之Android数据保存

简介: 本文参考Google官方文档http://developer.android.com/training/basics/data-storage/shared-preferences.html 在Android中最简单的数据保存方式就数——SharedPreferences。

本文参考Google官方文档http://developer.android.com/training/basics/data-storage/shared-preferences.html

在Android中最简单的数据保存方式就数——SharedPreferences。

如果你想要保存一些较小的键值对集合并且是简单的数据类型的话,使用SharedPreferences无疑是最好的选择,一个SharedPreferences对象指向的文件是一个包含键值对的XML文件,并且其本身提供了一些简单的方法供我们读写。其中SharedPreferences可以设置为私有和共享的。

第一步:获取SharedPreferences对象

1、通过getSharedPreferences(String name, int mode)方法返回一个SharedPreferences对象。如果name存在,则返回指向这个文件的SharedPreferences对象。如果name不存在,则创建新的文件,返回SharedPreferences对象。MODE是指文件的权限,常用的权限类型有三个:MODE_PRIVATE(私有--只能本程序访问)、MODE_WORLD_READABLE(公开读取权限--所有程序都可以读取)、MODE_WORLD_WRITEABLE(全局写权限--所有程序都能读写)。

2、通过getPreferences(int mode)方法返回SharedPreferences对象。这个SharedPreferences对象仅仅是当前Activity拥有,默认的name为当前Activity的类名。

第二步:获取SharedPreferences.Editor接口,实现对SharedPreferences的编辑

官方推荐:SharedPreferences文件命名推荐使用包名+名称,比如:"com.example.myapp.PREFERENCE_FILE_KEY"

SharedPreferences sp = this.getSharedPreferences("mySharedPreferences", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.commit();

第三步:读取SharedPreferences文件数据

读取数据和写数据是相对应的,通过方法getXxxx()将key-value中的数据取出。

SharedPreferences sp = MainActivity.this.getSharedPreferences("mySharedPreferences", Context.MODE_PRIVATE);
        String username = sp.getString("username", "moocher");
如果在SharedPreferences文件中没有找到USERNAME,默认会返回moocher值。

MainActivity.xml是通过getPreferences(int mode)方法创建的SharedPreferences文件。

mySharedPreferences.xml是通过getSharedPreferences(String name, int mode)方法创建的。

目录
相关文章
|
21天前
|
消息中间件 网络协议 Java
Android 开发中实现数据传递:广播和Handler
Android 开发中实现数据传递:广播和Handler
17 1
|
4月前
|
安全 API Android开发
Android网络和数据交互: 解释Retrofit库的作用。
Android网络和数据交互: 解释Retrofit库的作用。
41 0
|
5月前
|
XML 物联网 API
Android Ble蓝牙App(五)数据操作
Android Ble蓝牙App(五)数据操作
126 0
|
4天前
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
14 2
|
7天前
|
Java Linux API
统计android设备的网络数据使用量
统计android设备的网络数据使用量
14 0
|
4天前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
10 3
|
4天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
|
28天前
|
Android开发 开发者
Android网络和数据交互: 请解释Android中的AsyncTask的作用。
Android's AsyncTask simplifies asynchronous tasks for brief background work, bridging UI and worker threads. It involves execute() for starting tasks, doInBackground() for background execution, publishProgress() for progress updates, and onPostExecute() for returning results to the main thread.
12 0
|
28天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
14 0
|
1月前
|
JSON 安全 Java
Android网络部分-----网络数据请求、解析
Android网络部分-----网络数据请求、解析
Android网络部分-----网络数据请求、解析