Android SharePreferences的学习笔记

有些时候我们需要在应用程序中保存少量的数据,而且这些数据格式都比较简单,一般都是key-values对应,比如应用程序中的各种配置信息(比如游戏是否打开音效、是否使用震动效果),针对此,Android提供了SharePreferences来进行保存。

-SharePreferences接口主要负责读取应用程序的Preferences数据,它提供了如下常用方法来访问SharePreferences中的Key-Values对。

-boolean contains(String key):判断SharePreference是否包含特定的key的数据

-abstract  Map<String,?>getAll:获取SharePreferences数据里全部的key-values对

-boolean getXXX(String key,xxx defValue):获取SharePreferences数据指定key对应的value。如果该key不存在,则返回默认值defValue。其中xxx可以是boolean、float、int、long、String等各种基本数据类型的值。

SharePreferences接口本身并没有提供写入数据的能力,而是通过SharePreferences调用edit()方法即可获取它所对应的Editor堆箱。Editor提供了如下方法来向SharePreferences写入数据。

-SharePreferences.Editor clear():清空SharePreferences里所有的数据

-SharePreferences.Editor putXXX(String key,XXX values):向SharePreferences存入指定key对应的数据。其中XXX可以为:boolean float  int long String等各种基本类型的值。

-SharePreferences.Editor remove(String key):删除SharePreferences里指定key对应的数据项。

-boolean commit():当Editor编辑完成后,调用该方法提交修改。


SharePreferences本身是一个接口,程序无法直接创建SharePreferences实例,只能通过Context提供的getSharePreferences(String name,int mode)方法来获取SharePreferences实例,该方法的第二个参支持如下几个值:

-Context.MODE_PRIVATE:指定该SharePreferences数据只能被本应用程序读写

-Context.MODE_WORLD_READABLE:指定该SharePreferences数据能被其他应用程序都,但是不能写

-Context.MODE_WORLD_WRITEABLE:指定该SharePreferences数据能被其他应用程序读写

下面是SharePreferences的存储位置和格式的代码:

布局文件如下:

<pre name="code" class="java"><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.sharepreferencestest.MainActivity">

    <Button
        android:id="@+id/readSharePreferencesDATA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取数据"
        android:onClick="readSharePreferencesDATA"/>

    <Button
        android:id="@+id/writeSharePreferenceDATA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/readSharePreferencesDATA"
        android:onClick="writeSharePreferenceDATA"
        android:text="写入数据"/>
</RelativeLayout>

 

MainActivity的代码如下:
<pre name="code" class="java">package com.example.administrator.sharepreferencestest;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPreferences=getSharedPreferences("wkeyword", MODE_PRIVATE);
        editor=sharedPreferences.edit();
//        Button read= (Button) findViewById(R.id.readSharePreferencesDATA);
//        Button write= (Button) findViewById(R.id.writeSharePreferenceDATA);

    }

    public void readSharePreferencesDATA(View view) {
        String time=sharedPreferences.getString("time",null);
        int randNum=sharedPreferences.getInt("random", 0);
        String result=time==null?"您暂时还未写入数据":"写入时间为"+time+"\n上次生成的随机数为:"+randNum;
        Toast.makeText(this,result,Toast.LENGTH_SHORT).show();

    }

    public void writeSharePreferenceDATA(View view) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");
        editor.putString("time",sdf.format(new Date()));
        editor.putInt("random",(int)(Math.random()*100));
        editor.commit();
    }
}

生成的文件内容如下:
 
<pre name="code" class="html"><?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="time">2016年03月26日12:46:28</string>
    <int name="random" value="30" />
</map>


 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值