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 ...
随机推荐
- clique
[题目描述]数轴上有 n 个点,第 i 个点的坐标为 xi,权值为 wi.两个点 i,j 之间存在一条边当且仅当 abs(xi-xj)>=wi+wj.你需要求出这张图的最大团的点数.(团就是两两 ...
- [SDOI2005]反素数
题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6 ...
- Linux input子系统 io控制字段【转】
转自:http://www.cnblogs.com/leaven/archive/2011/02/12/1952793.html http://blog.csdn.net/guoshaobei/arc ...
- 软件测试人员在工作中如何运用Linux
从事过软件测试的小伙们就会明白会使用Linux是多么重要的一件事,工作时需要用到,面试时会被问到,简历中需要写到. 对于软件测试人员来说,不需要你多么熟练使用Linux所有命令,也不需要你对Linux ...
- 【iOS】苹果开发者账号申请
[1]首先登陆苹果开发者中心:https://developer.apple.com/programs/ 如图有一个按钮enroll,意思是苹果开发者报名(说白了就是要交钱,好让你具备APP测试和上线 ...
- Python3玩转儿 机器学习(4)
jupyternotebook 的使用方法¶ 最基本的使用¶ In [1]: 1+2 Out[1]: 3 菜单树¶ File¶ |------> New Notebook --- ...
- 阿里 & 酷家乐:实习生面试
最近海投了十家公司,暂时有阿里两面(已凉).酷家乐两面(大概凉了).网易一面.前两个都是基础知识发挥得还可以,两家公司二面都凉凉. 阿里一面(3.21 26min) 刚好买了中饭回宿舍打开正准备吃的时 ...
- monitoring with Prometheus
Prometheus是一款开源的监控工具,支持k8s metrics的数据格式,同时也支持通过k8s api进行服务发现从而实现对自定义的metrics进行监控.下面通过一个示例来介绍如何将Prome ...
- Jvm原理剖析与调优之内存结构
一些不得不说的概念 JVM JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟机包括一套字节码指令集.一组寄存器.一个栈.一个 ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...