Detailed explanation of SharedPreferences usage

690110ecf1733aeacc448ba57c0d0746.gif

Learn with you for lifeXi, this is Programmer Android

Recommended classic articles. By reading this article, you will gain the following knowledge points:

1. Introduction to SharedPreferences
2. How SharedPreferences saves data
3. How SharedPreferences reads data
4. Summary of how to use SharePerference Utils encapsulation class
5. SharedPreferences data storage location

1. Introduction to SharedPreferences

SharedPreferencesIt is Androidan interface class and Android a method of data storage (save internally). Mainly *.xmlsaved under Android in the form of  /data/data/com.***包名/shared_prefs, SharedPreferences the class provides a general framework so that users can save and retrieve key-value pairs of primitive data types. The primitive data types are as follows: Boolean, Int, Float, Long, String.

1. SharedPreferences The usage method is as follows:

  1. xmlCreate a file to save data

  2. Save data to Editor file usingxml

  3. commit()save data

  4. xmlsave place
    /data/data/com.***包名/shared_prefs

2. SharedPreferences method of saving data

Mainly use methods such as putBoolean() and  putString()and putInt()to add values.

3. How to read data in SharedPreferences

Mainly use  getBoolean()and  getString(), getInt()etc. to obtain saved data

4. Summary of how to use SharePerference Utils encapsulation class

1.Remove SharePerference saved values

private static SharedPreferences sp;
    private static String SPXMLNAME = "sp_config";

    /**
     * @param ctx
     *            上下文环境
     * @param key
     *            要从config.xml移除节点的name的名称
     */
    public static void removeKey(Context ctx, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().remove(key).commit();
    }

2. Save and get the Boolean type value method

// 1,存储boolean变量方法
    public static void putBoolean(Context ctx, String key, boolean value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    // 2,读取boolean变量方法
    public static boolean getBoolean(Context ctx, String key, boolean defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

3. Save and get Stringtype value method

public static void putString(Context ctx, String key, String value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).commit();
    }

    public static String getString(Context ctx, String key, String defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

4. Save and get Inttype value method

//
    public static void putInt(Context ctx, String key, int value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(Context ctx, String key, int defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defValue);
    }

5. SharePerferenceUtils

package com.programandroid.Utils;

import android.content.Context;
import android.content.SharedPreferences;

/*
 * SharePerferenceUtils.java
 *
 *  Created on: 2017-9-24
 *      Author: wangjie
 * 
 *  Welcome attention to weixin public number get more info
 *
 *  WeiXin Public Number : ProgramAndroid
 *  微信公众号 :程序员Android
 *
 */
public class SharePerferenceUtils {

    private static SharedPreferences sp;
    private static String SPXMLNAME = "sp_config";

    /**
     * @param ctx
     *            上下文环境
     * @param key
     *            要从config.xml移除节点的name的名称
     */
    public static void removeKey(Context ctx, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().remove(key).commit();
    }

    // 1,存储boolean变量方法
    public static void putBoolean(Context ctx, String key, boolean value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    // 2,读取boolean变量方法
    public static boolean getBoolean(Context ctx, String key, boolean defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    public static void putString(Context ctx, String key, String value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).commit();
    }

    public static String getString(Context ctx, String key, String defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

    //
    public static void putInt(Context ctx, String key, int value) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(Context ctx, String key, int defValue) {
        // name存储文件名称
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defValue);
    }

}

6. The usage methods in Activity class are as follows:

  1. save data

The saving data calling method is as follows:

SharePerferenceUtils.putInt(getApplicationContext(), "int_key", 1);
  1. retrieve data

The calling method to obtain data is as follows:

SharePerferenceUtils.getString(getApplicationContext(), "string_key", "default_values");

5. SharedPreferences data storage location

SharedPreferencesSaved appinternally (/data/data/com.***包名/shared_prefs), when clearing APK data manually, the saved data will be cleared

The usage method so far  SharedPreferencesis basically completed.

references:

[Tencent Documentation] Android basic knowledge base
https://docs.qq.com/doc/DSWdKRWh1VnVHYWFP

Friendly recommendation:

Collection of useful information on Android development

At this point, this article has ended. The editor thinks the article is reprinted from the Internet and is excellent. You are welcome to click to read the original article and support the original author. If there is any infringement, please contact the editor to delete it. Your suggestions and corrections are welcome. We look forward to your attention and thank you for reading, thank you!

199480d61ff79066266c3789c11cdcd4.jpeg

Click to read the original article and like the boss!

Guess you like

Origin blog.csdn.net/wjky2014/article/details/132061390