12 SharedPreferences
SharedPreferences
创建方式
SharedPreferences preferences = getPreferences(Context context ,int mode);
参数1: 上下文
参数2:写入模式
SharedPreferences preferences = getPreferences(int mode);
参数:写入模式
注意:写入的文件名为 你当前类.xml *写入模式
Context.MODE_PRIVATE:数据只能被本应用程序读和写
Context.MODE_APPEND:新内容追加到原内容之后
Context.MODE_WORLD_READABLE:能被其他程序读 不能写
Context.MODE_WORLD_WRITEABLE:能被其他程序读和写
写入的路径
data/data/包名/shared_prefs/文件名.xml
写入类型举例:
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
//获取编辑者对象
Editor editor = preferences.edit();
editor.putBoolean("booleanType", true);
editor.putFloat("floatType", 1.0f);
editor.putInt("intType", 11);
editor.putLong("longType", 123l);
editor.putString("stringType", "七龙珠");
boolean bl = editor.commit();
//editor.apply();
写入时如果以下情况那么后者为准
editor.putFloat(“Type”, 1.0f);
editor.putInt(“Type”, 11);
也就是说值写入了int类型的Type –》11
提交数据两种方法
我们在put后必须要commit或者apply
* 相同点:
* 二者都是 提交preferences修改的数据
* 不同点:
* 1.apply() 没有返回值 commit() 有boolean 返回值
* 2.apply() 不知存储成功没 commit()知道存储成功还是失败
* 3.apply()先提交到内存 在提交的到磁盘 commit() 直接提交到磁盘
读取方法
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
boolean bl = preferences.getBoolean(“booleanType”, false);
float ft = preferences.getFloat(“floatType”, -1f);
int it = preferences.getInt(“intType”, -1);
long lg = preferences.getLong(“longType”, -1l);
String str = preferences.getString(“stringType”, “”);
代码示例
package com.qf.day12_storage_demo1;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//存数据
public void SaveClick(View v){
/**
* 获取SharedPreferences对象
* 参数1:存储数据文件的名称
* 参数2:数据的标记
* Context.MODE_PRIVATE:数据只能被本应用程序读和写
* Context.MODE_APPEND:新内容追加到原内容之后
* Context.MODE_WORLD_READABLE:能被其他程序读 不能写
* Context.MODE_WORLD_WRITEABLE:能被其他程序读和写
*
* 存储的位置:
* data/data/{包名}/shared_prefs/参数1.xml
*
*/
//SharedPreferences preferences = getSharedPreferences("info", Context.MODE_PRIVATE);
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
//获取编辑者对象
Editor editor = preferences.edit();
editor.putBoolean("booleanType", true);
editor.putFloat("floatType", 1.0f);
editor.putInt("intType", 11);
editor.putLong("longType", 123l);
editor.putString("stringType", "七龙珠");
//提交执行
boolean bl = editor.commit();
//异步提交
//editor.apply();
/**
* 相同点:
* 二者都是 提交preferences修改的数据
* 不同点:
* 1,apply() 没有返回值 commit() 有boolean 返回值
* 2,apply() 不知存储成功没 commit()知道存储成功还是失败
* 3,apply()先提交到内存 在提交的到磁盘 commit() 直接提交到磁盘
*/
}
/**
* getSharedPreferences("info", Context.MODE_PRIVATE);在当前类存 可以在其他类中取
* getPreferences(Context.MODE_PRIVATE); 当前类存 当前类取
* @param v
*/
//取数据
public void GetClick(View v){
//SharedPreferences preferences = getSharedPreferences("info", Context.MODE_PRIVATE);
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
boolean bl = preferences.getBoolean("booleanType", false);
float ft = preferences.getFloat("floatType", -1f);
int it = preferences.getInt("intType", -1);
long lg = preferences.getLong("longType", -1l);
String str = preferences.getString("stringType", "");
Toast.makeText(MainActivity.this, "bl="+bl+"=ft="+ft+"=it="+it+"=lg="+lg+"=str="+str, 0).show();
}
}
12 SharedPreferences的更多相关文章
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- Android之SharedPreferences数据存储
一.SharedPreferences保存数据介绍 如果有想要保存的相对较小键值集合,应使用SharedPreferences API.SharedPreferences对象指向包含键值对的文件并提供 ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- android数据存储之SharedPreferences
一.SharedPreferences简介 (1)SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activ ...
- 写了个SharedPreferences的工具类(带加密)
/* * Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com ) * * Licensed under the Apache License, V ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...
随机推荐
- 控制公司 Controlling Companies
题目描述 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.(此处略去一句废话)据说,如果至少满足了以下三个条件之一,公司A就可以控制公司B了: 公司A = 公司B. 公司A拥 ...
- hdu 5391 (数论)
Zball in Tina Town Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- MySql 时间操作实例
SELECT NOW(6) AS '当前时间精确到微秒'; SELECT UNIX_TIMESTAMP() AS '当前时间戳',UNIX_TIMESTAMP('2018-1-1') AS '转换成时 ...
- C语言 分支与循环 递推思想 穷举 流程的转移控制
条件语句 开关控制语句(SWITCH语句) 象坐电梯一样,break是按的楼层,不加break则会一直执行下去. 上面程序有细节BUG,边界测试输入-5,105时由于整除会得到错误的结果. 解决方法: ...
- 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- JavaScript正则表达式模式匹配(5)——特殊字符匹配、换行模式
特殊字符匹配 var pattern=/\[/; // 用\符号来转义正则里的特殊字符才能匹配 var str='['; alert(pattern.test(str)); 换行模式 var patt ...
- Sublime Text3时间戳查看转换插件的开发
平常配置表中,经常需要用到时间配置,比如活动开始结束.从可读性上,我们喜欢2017-04-27 17:00:00,从程序角度,我们喜欢用1493283600.前者是包含时区概念的,而后者市区无关,所以 ...
- FileOutputStream&FileInputStream&异常的使用
FileOutputStream&FileInputStream&异常的使用 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真 ...
- Python中capitalize()与title()的区别
capitalize()与title()都可以实现字符串首字母大写.主要区别在于:capitalize(): 字符串第一个字母大写title(): 字符串内的所有单词的首字母大写 例如: >&g ...
- python学习之路基础篇(第八篇)
一.作业(对象的封装) 要点分析 1.封装,对象中嵌套对象 2.pickle,load,切记,一定要先导入相关的类二.上节内容回顾和补充 面向对象基本知识: 1.类和对象的关系 2.三大特性: 封装 ...