- Android 系统概述
- 什么是智能手机?智能手机操作系统有哪些?
- Android(安卓)是什么?
- Android 系统架构(图解)
- Android 7 新特性介绍
- Android 开发环境搭建
- Android 开发环境搭建教程(图解)
- Android SDK 是什么?
- 通过 Android Studio 创建 Android 应用程序(附带解析)
- Android Studio 的基本调试
- Android 应用程序结构:Android 组件和资源详解
- Android 四大核心组件:Activity+Service+BroadcastReceiver+ContentProvider
- Android Activity 组件及其生命周期
- Android 资源(res 和 assets 目录)
- AndroidManifest.xml 配置文件详解
- Android App Widgets 组件详解
- Android 系统的进程和线程
- Android GUI(图形用户界面)开发
- Android View 和 ViewGroup
- Android 使用 XML 文件定义用户界面
- Android 五大布局:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout 和 TableLayout
- Android 创建 Widget 组件实例详解
- Android Button:按钮控件
- Android CheckBox:多选按钮控件
- Android RadioGroup:单选按钮控件
- Android TextView:文本框控件
- Android EditText:编辑框控件
- Android Spinner:下拉列表控件
- Android AutoCompleteTextView:自动完成文本框控件
- Android DatePicker 和 TimePicker:时间日期选择器
- Android ProgressBar:进度条控件
- Android ScrollView:滚动视图
- Android SeekBar:拖动条控件
- Android RatingBar:评价条控件
- Android ImageView 和 ImageButton:图片视图和图片按钮
- Android ImageSwitcher 和 Gallery:图片切换器和图库
- Android GridView:网格视图
- Android Tab:面板标签控件
- Android 菜单:选项菜单+上下文菜单+子菜单
- Android Bitmap(点阵图像、绘制图像)
- Android 对话框(AlertDialog+ProgressDialog)
- Android 多窗口模式(分屏模式)
- Android 事件监听器和回调方法
- Android 电话短信拍照开发教程
- Android Intent:不同应用程序之间通信的桥梁
- Android 实现拨打电话功能
- Android 实现接收短信和发送短信功能
- Android 调用相机进行拍照
- Android 多媒体开发
- Android Service 生命周期和使用方法
- Android BroadcastReceiver:接收广播
- Android 音频以及音频播放器开发实例
- Android MediaRecorder 录制音频
- Android 视频以及视频播放器实例
- Android Camera 相机以及相机程序开发实例
- Android MediaRecorder 录制视频
- Android 数据存储(SQLite 数据库、Android App 数据备份和恢复 )
- Android 数据存储之 SharedPreferences
- Android 数据存储之文件存储
- Android 使用 SQLite 数据库存数数据
- Android SQLite 数据库操作实例
- Android ContentProvider(内容提供者)
- Android 使用系统提供的 ContentProvider
- Android 自定义 ContentProvider
- Android 使用自定义 ContentProvider
- Google App Engine 是什么?
- Android APP 数据备份与恢复
- Android BackupAgent 实现数据备份与恢复
- Android BackupAgentHelper 实现数据备份与恢复
- Android 网络编程
- 借助 GPE 让 Android APP 和 Google App Engine 通信
- Android HTTP 通信
- Android HttpURLConnection 访问互联网资源
- Android 通过 GET 方式获取互联网资源
- Android 通过 POST 方式获取互联网资源
- Android Socket 编程(附带实例)
- Bluetooth(蓝牙)是什么?
- Android 蓝牙通信开发教程(详解版)
- Android 蓝牙通信开发实例演示
- WIFI(无线局域网)是什么?
- Android WiFi 开发(WiFi 编程)实例演示
- WIFI Direct(WIFI 直连)是什么?
- Android WIFI Direct 开发教程
- Android WIFI Direct 开发实例演示
- NFC(近场通信)是什么?
- Android NFC 开发教程
- Android USB 开发简介
- Android USB 附件模式开发教程
- Android USB 主机模式开发教程
- Android SIP 开发教程
- Android 智能传感器
- LBS(位置服务)和 GPS(全球定位系统)简介
- Android LBS 位置服务开发简介
- Android GPS 定位开发教程
- Google Map API 开发简介
- Android Google Map API Key 详细申请步骤
- Google Map API 开发 Android 地图应用实例
- Android 传感器开发教程
- Android 运动传感器开发教程
- Android 位置传感器开发教程
- Android 环境传感器开发教程
- Android 绘图(2D 绘图、3D 绘图)
- Android 2D 绘图开发简介
- Android 使用自定义 View 绘图
- Android 使用 Bitmap 绘图
- Android 使用 SurfaceView 绘制静态图像
- Android 使用 SurfaceView 绘制动态图像
- Android Drawable 开发简介
- Android 3D 绘图开发简介
- Android 硬件加速开发简介
- Android RenderScript 开发简介
- Android App 的国际化和本地化
- Android 国际化和本地化开发简介
- Android 手机区域设置详细步骤
- Android 未本地化应用开发教程
- Android 本地化应用开发教程
- Android 文本输入与复制粘贴
- Android 剪贴板框架开发简介
- Android 剪贴板数据转文本开发简介
- Android 复制粘贴开发教程
- Android Content Provider 复制复杂数据开发教程
- Android 一键复制粘贴开发实例
- Android 企业级应用开发
- Android 设备管理 API 概述
- Android 开发设备管理 API 应用教程
- Android 文本语音 API 开发教程
- Android TV 应用开发教程
- Android TV 应用开发实例
- Android 可穿戴设备应用开发教程
- Android 应用程序的发布
- Android 应用程序发布的步骤
- Android 为什么要为应用程序签名?
- Android 导出未签名应用程序开发步骤
- Android 生成签名文件开发教程
- Android 应用程序签名开发教程
- zipalign 工具优化应用程序开发简介
- Google Play Store( Android Market)发布简介
Android 数据存储之 SharedPreferences
无论是桌面应用程序还是 Android 手机应用程序,都会涉及数据的存储。下面我们将会详细介绍在 Android 中存储数据的相关知识。
在 Android 中应用程序存储的数据(包括文件)都属于应用程序私有,但同时也提供了 ContentProviders(数据共享),方便应用程序将私有的数据分享给其他程序使用。
数据存储方式共分为 5 种,分别为:
- SharedPreferences。
- 内部存储(Internal Storage)。
- 外部存储(External Storage)。
- SQLite 数据库存储。
- 网络存储。
其中,网络存储在本质上是对网络资源的获取和访问,其相关内容会在网络编程教程中进行介绍。
此外,Android 系统框架提供了 ContentProvider 来实现各种应用程序间持久化数据的共享。
下面我们先来介绍一下第一种 SharedPreferences。
SharedPreferences
SharedPreferences 是 Android 系统提供的一个通用的数据持久化框架,用于存储和读取 key-value 类型的原始基本数据对。
目前仅支持 boolean、float、int、long 和 string 等基本类型的存储,对于自定义的复合数据类型,是无法使用 SharedPreferences 进行存储的。
SharedPreferences 主要用于存储系统的配置信息,类似于 Windows 下常用的 .ini 文件。
例如上次登录的用户名、上次最后设置的信息等,通过保存上一次用户所做的修改或者自定义参数设定,当再次启动程序后依然保持原有设置。它是用键值对的方式存储的,方便管理写入和读取。
使用 SharedPreferences 的步骤如下:
1) 获取 Preferences
每个 Activity 默认都有一个 SharedPreferences 对象,获取 SharedPreferences 对象的方法有两种:
1)SharedPreferences getSharedPreferences(String name, int mode)。
使用该方法获取 name 指定的 SharedPreferences 对象,并获取对该 SharedPreferences 对象的读写控制权。
当应用程序中可能使用到多个 SharedPreferences 时使用该方法。
2)SharedPreferences getPreferences(int mode)。
当应用程序中仅需要一个 SharedPreferences 对象时,使用该方法获取当前 Activity 对应的 SharedPreferences,而不需要指定 SharedPreferences 的名字。
其中,参数 mode 有 4 种取值,分别是:
- MODE_PRIVATE:默认方式,只能被创建的应用程序或者与创建的应用程序具有相同用户 ID 的应用程序访问。
- MODE_WORLD_READABLE:允许其他应用程序对该 SharedPreferences 文件进行读操作。
- MODE_WORLD_WRITEABLE:允许其他应用程序对该 SharedPreferences 文件进行写操作。
- MODE_MULTI_PROCESS:在多进程应用程序中,当多个进程都对同一个 SharedPreferences 进行访问时,该文件的每次修改都会被重新核对。
2) 获取 SharedPreferences.Editor
调用 edit() 方法获取 SharedPreferences.Editor,SharedPreferences 通过该接口对其内容进行更新。
3) 更新 SharedPreferences
通过 SharedPreferences.Editor 接口提供的 put 方法对 SharedPreferences 进行更新。
例如使用 putBoolean(String key, boolean value)、putFloat(String key, float value) 等方法将相应数据类型的数据与其 key 对应起来。
4) 提交
调用 SharedPreferences.Editor 的 commit() 方法将更新提交到 SharedPreferences 中。
使用 SharedPreferences
实例 SharedPreferencesDemo 演示了 SharedPreferences 对象的使用方法。
该实例的运行效果如图 1 所示。
图 1 SharedPreferencesDemo
当用户在该实例运行时,在文本框中输入电话号码和所在城市,例如 13800000000 和 beijing,单击回退按钮退出应用程序时,该应用程序将相关信息写入其对应的 SharedPreferences 中。
当用户再次启动该应用程序时,之前填写到文本框内的信息会被从 SharedPreferences 中读取并显示出来,以方便用户修改。
实例 SharedPreferencesDemo 中的布局文件 main.xml 中放置了三个 TextView 和两个 EditText,其中两个 EditText 按照 TextView 的要求输入电话号码和城市。其代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="使用 Shared Preferences 存储程序信息" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="您的电话号码" /> <EditText android:id="@+id/phone_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入电话号码" /> <EditText android:id="@+id/city_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入城市名称" /> </LinearLayout>
实例 SharedPreferencesDemo 中 AndroidManifest.xml 的代码如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="introduction.android.SharedPreferencesDemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="14" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
实例 SharedPreferencesDemo 中 MainActivity.java 的代码如下:
import javax.security.auth.PrivateCredentialPermission; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.EditText; public class MainActivity extends Activity { private EditText phoneText, cityText; private String phone, city; public static final String SET_INFO = "SET_Info"; public static final String PHONE = "PHONE"; public static final String CITY = "CITY"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phoneText = (EditText) findViewById(R.id.phone_text); cityText = (EditText) findViewById(R.id.city_text); /*获取 Shared Preferences 对象*/ SharedPreferences setinfo = getPreferences(Activity.MODE_PRIVATE); /*取出保存的电话号码和地址信息*/ phone = setinfo.getString(PHONE, ""); city = setinfo.getString(CITY, ""); /*将取出的信息分别放在对应的 EditText 中*/ phoneText.setText(phone); cityText.setText(city); } @Override protected void onStop() { SharedPreferences setinfo = getPreferences(Activity.MODE_PRIVATE); setinfo.edit() .putString(PHONE, phoneText.getText().toString()) .putString(CITY, cityText.getText().toString()) .commit(); super.onStop(); } }
该 Activity 在启动时通过 onCreate() 方法从其 SharedPreferences 中获取相应数据,在 onStop() 方法中将相应数据写入 SharedPreferences 中,其中:
SharedPreferences setinfo=getPreferences(Activity.MODE_PRIVATE);
用于获取当前 Activity 默认的 SharedPreferences 对象,该对象没有名字。当然,也可以通过 getSharedPreferences(String name, int mode) 方法来创建并获取一个带有名字的 SharedPreferences。
该 SharedPreferences 被创建后,可以在应用程序的包路径下,即 data/data/<your package name>/shared_prefs 文件夹下找到该文件。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论