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. hdu 4812 DTree (点分治)

    D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  2. HDU 4641 K-string

    Description Given a string S. K-string is the sub-string of S and it appear in the S at least K time ...

  3. centos7安装nginx必要环境

    安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行 , 在安装nginx前还要安装以下的环境包 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc ...

  4. Linux学习之CentOS(六)---mount挂载设备(u盘,光盘,iso等 )

    对于新手学习,mount 命令,一定会有很多疑问.其实我想疑问来源更多的是对linux系统本身特殊性了解问题. linux是基于文件系统,所有的设备都会对应于:/dev/下面的设备.如: [cheng ...

  5. 9.QT-标准对话框

    Qt提供的可复用的标准对话框,全部继承自QDialog类,如下图所示: QMessageBox:信息对话框,用于显示信息.询问问题等: QFileDialog:文件对话框 QColorDialog:颜 ...

  6. Mysql锁机制--间隙锁的危害

    Mysql 系列文章主页 =============== 1 准备数据 1.1 建表 DROP TABLE IF EXISTS employee; CREATE TABLE IF NOT EXISTS ...

  7. Postgresql合并年月日、月份和日期左侧补零

    在写一个统计查询的 SQL 语句时,需要根据年.月.日分组,但要求返回的字段是日期格式:yyyy年MM月dd日.刚开始我的做法是返回年.月.日,然后再手动拼接年月日,而且还要判断月份和日期是否为个位数 ...

  8. 120. Triangle(中等)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. ubuntu安装fat32和exfat文件系统支持

    vftp(fat32) apt install -y dosfstools exfat apt install -y exfat-fuse exfat-utils

  10. Jeff Atwood倾情推荐——程序员必读之书

    英文版:<Code Complete 2>中文版:<代码大全(第二版)>作者:Steve McConnell译者:金戈  汤凌  陈硕  张菲出版社:电子工业出版社出版日期:2 ...