SharePerference Use Details

This article mainly introduces some of the knowledge points in Android development. By reading this article, you will get the following.

1. Use of Shared Preferences
2. Shared Preferences Method of Storing Data
3. Shared Preferences Method of Reading Data
4. Summarize the usage of Shared Preferences Utils encapsulation class
5. Summary

Shared Preferences is an interface class of Android and a method of storing Android data. The SharedPreferences class provides a general framework for users to save and retrieve key-value pairs of the original data types, which are as follows: Boolean, Int, Float, Long, String.

1. Use of Shared Preferences

Shared Preferences is used as follows:

Create xml files to save data
Use Editor to save data to xml files
commit() saves data
xml storage area
/ data/data/com. *** package name/shared_prefs

2. Shared Preferences Method of Storing Data

Put Boolean () and putString(), putInt() are used to add values.

3. Shared Preferences Method of Reading Data

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

4. Summarize the usage of SharePerference Utils encapsulation class

Remove SharePerference saved values

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

    /**
     * @param ctx
     *            Context environment
     * @param key
     *            To remove the name of the node from config.xml
     */
    public static void removeKey(Context ctx, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().remove(key).commit();
    }

Save and get Boolean type value methods

    // 1. Storing boolean variables
    public static void putBoolean(Context ctx, String key, boolean value) {
        // name Storage File name
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    // 2. Read boolean variable method
    public static boolean getBoolean(Context ctx, String key, boolean defValue) {
        // name Storage File name
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

Save, get String type value method

  public static void putString(Context ctx, String key, String value) {
        // name Storage File 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 Storage File name
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }

Save, get Int type value method

 //
    public static void putInt(Context ctx, String key, int value) {
        // name Storage File 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 Storage File name
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defValue);
    }

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
 *  Wechat Public Number: Programmer Android
 *
 */
public class SharePerferenceUtils {

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

    /**
     * @param ctx
     *            Context environment
     * @param key
     *            To remove the name of the node from config.xml
     */
    public static void removeKey(Context ctx, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().remove(key).commit();
    }

    // 1. Storing boolean variables
    public static void putBoolean(Context ctx, String key, boolean value) {
        // name Storage File name
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    // 2. Read boolean variable method
    public static boolean getBoolean(Context ctx, String key, boolean defValue) {
        // name Storage File 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 Storage File 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 Storage File 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 Storage File 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 Storage File name
        if (sp == null) {
            sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defValue);
    }

}

The following methods are used in the Activity class:

1. Save data

The method of saving data calls is as follows:

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

2. Data acquisition

The method for obtaining data calls is as follows:

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

So far, the use of Shared Preferences has been basically completed.

## 5. Summary

Shared Preferences are stored inside app (/ data/data/com. *** package name / shared_prefs), and when APK data is removed manually, the saved data is removed.

Keywords: Android xml Java

Added by severndigital on Tue, 11 Jun 2019 01:23:57 +0300