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. APIO dispatching

    题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...

  2. hihocoder 1388 fft循环矩阵

    #1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...

  3. hdu 1166 线段树(sum+单点修改)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. Python3 中 random模块

    Python中的random模块用于生成随机数. 下面具体介绍random模块的功能: 1.random.random() #用于生成一个0到1的 随机浮点数:0<= n < 1.0 im ...

  5. OCP 认证考试报名费技巧题库051052053解析合格线

    本人于2017年4月22日通过参加OCP考试,第一次参加,一天之内考了三门,三门一次性通过,052 - 95% ,053 - 86% ,051 - 100% 一.关于考试考试报名费: 052:158$ ...

  6. tf.contrib.seq2seq.sequence_loss example:seqence loss 实例代码

    #!/usr/bin/env python # -*- coding: utf-8 -*- import tensorflow as tf import numpy as np params=np.r ...

  7. MyBatis 传入参数之parameterType

      在MyBatis的select,insert,update,delete这些元素中都提到了parameterType这个属性.MyBatis现在使用parameterType有基本类型和JAVA复 ...

  8. 报表生成工具 —— iText

    十二月份一直在做生成报表的功能,中间踩了不少坑,刚好今天有时间,写篇博客记录一下. 一.iText 简介 iText 是著名开放源码站点 sourceforge 的一个项目,是一个用于生成 PDF 文 ...

  9. Maven的pom.xml文件结构之基本配置packaging和多模块聚合结构(微服务)

    1. packaging packaging给出了项目的打包类型,即作为项目的发布形式,其可能的类型.在Maven 3中,其可用的打包类型如下: jar,默认类型 war ejb ear rar pa ...

  10. vue-router实现登录和跳转到指定页面,vue-router 传参

    定义路由的时候可以配置 meta 字段: const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, childr ...