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();


}

}

原文地址:https://www.cnblogs.com/muyuge/p/6152264.html