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 ...
随机推荐
- C++Primer学习——未定义行为
定义: 主要是求值顺序的问题 int i = f1() + f2(); //我们无法知道是f1 还是 f2先被调用 而且求值顺序和优先级和结合律无关,比如: f() + g()*h( ...
- React 深入系列4:组件的生命周期
文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列4:组件的生命周期 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助 ...
- Windows提示dll组件丢失
我们在运行一些软件时,常常会遇到这种问题.下面就来提供解决办法: 登陆网址:www.dll-files.com. 找到页面的搜索部分,并且进行相关搜索: 下图显示了相关的dll下载链接. 下载解压即可 ...
- Python3中无法导入ssl模块的解决办法
这个问题,已经困扰我好几天了,本萌新刚开始接触python,想爬取几个网页试试,发现urllib无法识别https,百度后才知道要导入ssl模块,可是发现又报错了. 本人实在无法理解为什么会报错,因为 ...
- 在 Mac OS X 上安装 TensorFlow
在 Mac OS X 上安装 TensorFlow 这个文档说明了如何在 Mac OS X 上安装 TensorFlow. 注意:从 1.2 版本开始,在 Mac OS X 上 TensorFlow ...
- Oracle 导入、导出DMP(备份)文件
首先说明dmp文件: Oracle备份文件是以dmp结尾,这种文件是oracle的逻辑备份文件,常用于数据库逻辑备份,数据库迁移等操作. 一.Oracle导入备份文件步骤:我用的是Oracle 11g ...
- Spring cloud 学习资料整理
推荐博客 纯洁的微笑 程序猿DD liaokailin的专栏 周立 Spring Cloud 方志朋 Spring Cloud 专栏 许进 跟我学Spring Cloud 推荐网站 Spring Cl ...
- JMeter如何和Charles进行接口测试
什么是接口测试,接口测试到底如何开展,我相信任何一个软件测试人员都会有这样的疑问, 这里我以接口测试自动化平台的登录接口进行讲解. 一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测 ...
- RandomAccessFile&IO流&排序&方法论
RandomAccessFile&IO流&排序&方法论 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真真切切的历史. ...
- C++笔记003:从一个小程序开始
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 安装好VS2010后,从第一个小程序开始. 在学习C语言时,我首先输出了一个程序员非常熟悉的对这个世界的问候:hello world! ...