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的更多相关文章

  1. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  2. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  3. 在mybatis中写sql语句的一些体会

    本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...

  4. Android之SharedPreferences数据存储

    一.SharedPreferences保存数据介绍 如果有想要保存的相对较小键值集合,应使用SharedPreferences API.SharedPreferences对象指向包含键值对的文件并提供 ...

  5. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  6. android数据存储之SharedPreferences

    一.SharedPreferences简介      (1)SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activ ...

  7. 写了个SharedPreferences的工具类(带加密)

    /* * Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com ) * * Licensed under the Apache License, V ...

  8. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences

    除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...

  9. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider

    ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...

随机推荐

  1. IO复用

    IO复用:使得程序能同时监听多个文件描述符 select: select在一段指定的时间内,监听用户感兴趣的文件描述符的 读.写.异常事件. select(int nfds,fd_set* readf ...

  2. Blocks

    Description solution 这题和之前做过的一题的一个套路非常类似:把不是更优的决策给去掉,使得序列变得具有单调性,分析这题: 发现如果两个右端点 \(i\),\(j\) 满足 \(su ...

  3. bzoj3702二叉树 线段树合并

    3702: 二叉树 Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 600  Solved: 272[Submit][Status][Discuss] ...

  4. 解读Raft(一 算法基础)

    最近工作中讨论到了Raft协议相关的一些问题,正好之前读过多次Raft协议的那paper,所以趁着讨论做一次总结整理. 我会将Raft协议拆成四个部分去总结: 算法基础 选举和日志复制 安全性 节点变 ...

  5. Python与C的简单比较(Python3.0)

    Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...

  6. Python中的条件和循环语句

    条件和循环语句 1. 条件语句 if单用 格式:if 条件表达式 例如:if 5 > 3: print('True') >>> 'True' #当条件满足时才会执行上述操作. ...

  7. glusterfs 4.0.1 event模块 分析笔记1

    1. 前言 在C语言i中,存储变量的结构体加上一组函数指针,大概就可以算是一个对象模型了:如果将一组函数指针捆绑为结构体, 后期根据配置或者环境需要绑定到不同实现模块中的一组函数,可以认为是C语言面对 ...

  8. Arduino抢答器

    0.部分需要掌握的知识点和注意事项 (1)面包板的结构 (2)按键的结构:按键按下时,左右两侧连通:按键松开后,左右两侧断开,但1号与2号相连,3号与4号相连,即按键松开时,同侧不相连,相连不同侧. ...

  9. mysql 拼接

    SELECT  RTRIM(CONCAT(belong_master_ip ,'(',host_name,')')) AS cloudIP  FROM `cloud_master_cfg`

  10. jQuery extend 方法使用 (转)

    方法介绍 jQuery 的 API 手册中,extend 方法挂载在 jQuery 和 jQuery.fn 两个不同的对象上,但在 jQuery 内部代码实现的是相同的,只是功能各不相同. 先看看官方 ...