Android数据存储之SharedPreferences存储
安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串、整形、布尔型等。这些配置最后会保存在一个XML文件中,每次打开应用时,这些保存的信息就会被加载进来,我们也可以在“管理应用程序”中将这些缓存数据清除。
SharedPreferences接口的常用方法如下:
| No | 方法 | 类型 | 描述 |
| 1 | public abstract SharedPreferences.Editor edit() | 普通 | 使其可编辑,并获得编辑器对象 |
| 2 | public abstract boolean contains(String key) | 普通 | 判断key是否存在 |
| 3 | public abstract Map<String,?>getAll() | 普通 | 获得全部数据 |
| 4 | public abstract boolean getBoolean(String key, boolean defValue) | 普通 | 获得boolean值,若无,则设为defValue |
| 5 | public abstract float getFloat(String key, float defValue) | 普通 | 获得float值,若无,则设为defValue |
| 6 | puclic abstract int getInt(String key, int defValue) | 普通 | 获得int值,若无,则设为defValue |
| 7 | public abstract long getLong(String key, long defValue) | 普通 | 获得long值,若无,则设为defValue |
| 8 | public abstract String getString(String key, String defValue) | 普通 | 获得Sting值,若无,则设为defValue |
SharedPreferences接口的方法getXxx()用来获取已经存储的值,而要写入新的名值对则需要获得它的编辑器接口SharedPreferes.Editor接口,即调用SharedPreferences接口的edit()方法即可获得。
SharedPreferences.Editor接口的常用方法如下:
| No | 方法 | 类型 | 描述 |
| 1 | public abstract SharedPreferences.Editor clear() | 普通 | 清除所有数据 |
| 2 | public abstract boolean commit() | 普通 | 提交更新的数据 |
| 3 | public abstract SharedPreferences.Editor putBoolean(String key, Boolean value) | 普通 | 保存boolean型数据 |
| 4 | public abstract SharedPreferences.Editor putFloat(String key, float value) | 普通 | 保存float型数据 |
| 5 | public abstract SharedPreferences.Editor putInt(String key, int value) | 普通 | 保存int型数据 |
| 6 | public abstract SharedPreferences.Editor putLong(String key, long value) | 普通 | 保存long型数据 |
| 7 | public abstract SharedPreferences.Editor putString(String key, String value) | 普通 | 保存String型数据 |
| 8 | public abstract SharedPreferences.Editor remove(String key) | 普通 | 删除指定key的数据 |
由于SharedPreferences和SharedPreferences.Editor都是接口,所以要想获得他们的实例化对象,还需要Activity类的方法和常量的支持
public static final int MODE_PRIVATE, public static final int MODE_WORLD_READBALE, public static final int MODE_WORLD_WRITEBALE常量和public SharedPreferences getSharedPreferences(String name, int mode)(其中name指保存的文件名称,mode指操作的模式)。
下面以一个实例加以说明:
package com.wl.testsharedpreferences; import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { Button btn1,btn2;
SharedPreferences sp = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(this);
btn2.setOnClickListener(this); sp = getSharedPreferences("mysp", Activity.MODE_PRIVATE); //获得SharedPreferences实例
} @Override
public void onClick(View view) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.tv);
if(view == btn1) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("author", "xiaowang");
editor.putInt("age", 25);
editor.commit();
tv.setText("Write successfully!");
} else if(view == btn2) {
tv.setText(sp.getString("author", "xiaozhang") + "," + sp.getInt("age", 20));
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> <Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"/> //string/btn1为"写名值对" <Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"/> //string/btn2为"读名值对" </LinearLayout>
从以上例子可以很容易的掌握SharedPreferences的用法。
注:当我们写入名值对后,一定记得要调用commit()函数,不然所写入的并未提交,也就无从读出了。
Android数据存储之SharedPreferences存储的更多相关文章
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
SQLite是一种转为嵌入式设备设计的轻型数据库,其只有五种数据类型,分别是: NULL: 空值 INTEGER: 整数 REAL: 浮点数 TEXT: 字符串 BLOB: 大数据 在SQLite中, ...
- (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android入门(九)文件存储与SharedPreferences存储
原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...
- Android数据的四种存储方式
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...
- [Android]Android数据的四种存储方式
存储方式 Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/d ...
- [转][Android]Android数据的四种存储方式
android.database.sqlite类 SQLiteQueryBuilder java.lang.Object android.database.sqlite.SQLiteQueryBuil ...
随机推荐
- 转 夕甲甲:孔乙己之 C++ 版
欧欧匹代码的格局,是和别的编程模式不同的:首先要有一个构造函数:基类里只定义了函数的形式,可以随时通过派生增加不同的实现.那些程序员们,每每学会了继承和多态,便可以接一个项目,——这是十年前的事,现在 ...
- Angularjs学习笔记(五)----显示和格式化数据
一.引用指令 在AngularJS的文档中,所有指令的名字以驼峰命名法.而在模板中,则需要以蛇形命名法.可以以冒号分割(ng:model)或下划线分割(ng_model),更常见的是以ng-model ...
- Altium Designer PCB双面板制作打印操作步骤
Altium Designer PCB双面板制作打印操作步骤百度知道:http://jingyan.baidu.com/article/335530da83441c19cb41c3db.html?st ...
- Odoo Web Service API
来自 Odoo Web服务暴露出相关的服务,路由分别是 /xmlrpc/ /xmlrpc/2/ /jsonrpc 根据 services 调用 后端对应服务的 方法method [定义 openerp ...
- [转载] Java高新技术第一篇:类加载器详解
本文转载自: http://blog.csdn.net/jiangwei0910410003/article/details/17733153 首先来了解一下字节码和class文件的区别: 我们知道, ...
- Android中的事件传递机制
Android源码版本:API Level 19(Android 4.4) Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和 ...
- laravel(一):如何安装laravel
1.前提条件 本文针对想从零开始开发 Laravel 程序的初学者,不需要预先具备任何的 Laravel 使用经验.不过,为了能顺利阅读,还是需要事先安装好一些软件: PHP 5.4 及以上版本 包管 ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- C# 遍历文件夹下所有子文件夹中的文件,得到文件名
假设a文件夹在F盘下,代码如下.将文件名输出到一个ListBox中using System.Data;using System.Drawing;using System.Linq;using Syst ...
- VueJS取得URL参数
vuejs取得URL中参数的值 地址:http://localhost:3333/#/index?id=001 结果:001 console.log(this.$route.query.id)