PreferencesUtils【SharedPreferences操作工具類】


版權聲明:本文為HaiyuKing原創文章,轉載請注明出處!

前言

可以替代ACache用來保存用戶名、密碼。

相較於Acache,不存在使用獵豹清理大師進行垃圾清理的時候把緩存的數據清理掉的問題。

效果圖

代碼分析

需要注意的是命名的KEY值直接在PreferencesUtils類中聲明了。可以根據項目要求,在Globals類文件(或者在類似文件(用於存放全局變量和公共方法))中聲明。

使用步驟

一、項目組織結構圖

注意事項:

1、導入類文件后需要change包名以及重新import R文件路徑

2、Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),如果項目中存在,則復制里面的內容,不要整個覆蓋

二、導入步驟

將PreferencesUtils復制到項目中

package com.why.project.preferencesutilsdemo.utils;

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

/**
 * Used 臨時存儲數據操作類(全)
 */
public class PreferencesUtils {
    public static String PREFERENCE_NAME = "why";

    /**用戶名的key值*/
    public static String USERNAME = "username";

    /**存儲字符串*/
    public static boolean putString(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }
    /**讀取字符串*/
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }
    /**讀取字符串(帶默認值的)*/
    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getString(key, defaultValue);
    }
    /**存儲整型數字*/
    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }
    /**讀取整型數字*/
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }
    /**讀取整型數字(帶默認值的)*/
    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getInt(key, defaultValue);
    }
    /**存儲長整型數字*/
    public static boolean putLong(Context context, String key, long value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }
    /**讀取長整型數字*/
    public static long getLong(Context context, String key) {
        return getLong(context, key, 0xffffffff);
    }
    /**讀取長整型數字(帶默認值的)*/
    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getLong(key, defaultValue);
    }
    /**存儲Float數字*/
    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }
    /**讀取Float數字*/
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1.0f);
    }
    /**讀取Float數字(帶默認值的)*/
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getFloat(key, defaultValue);
    }
    /**存儲boolean類型數據*/
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }
    /**讀取boolean類型數據*/
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }
    /**讀取boolean類型數據(帶默認值的)*/
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getBoolean(key, defaultValue);
    }
    /**清除數據*/
    public static boolean clearPreferences(Context context) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        return editor.commit();
    }
}
PreferencesUtils.java

三、使用方法

package com.why.project.preferencesutilsdemo;

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

import com.why.project.preferencesutilsdemo.utils.PreferencesUtils;

public class MainActivity extends AppCompatActivity {

    private EditText mUsernameEdt;
    private Button mLoginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initDatas();
        initEvents();
    }

    //初始化控件
    private void initViews(){
        mUsernameEdt = (EditText) findViewById(R.id.edt_username);
        mLoginBtn = (Button) findViewById(R.id.btn_login);
    }

    //初始化數據
    private void initDatas(){
        //判斷是否緩存了用戶名,如果是的話,讀取緩存的用戶名並填充到輸入框中
        initNamePwdFromCache();
    }

    //初始化事件
    private void initEvents(){
        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = mUsernameEdt.getText().toString();
                //緩存用戶名
                PreferencesUtils.putString(MainActivity.this,PreferencesUtils.USERNAME,userName);
                Toast.makeText(MainActivity.this,"已緩存用戶名:"+userName,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 從緩存中查詢用戶名是否保存,並加載用戶名
     * */
    private void initNamePwdFromCache() {
        //有緩存文件
        String userNameCache = PreferencesUtils.getString(MainActivity.this,PreferencesUtils.USERNAME); if (userNameCache != null) {
            mUsernameEdt.setText(userNameCache);
            Toast.makeText(MainActivity.this,"已加載緩存的用戶名:"+userNameCache,Toast.LENGTH_SHORT).show();
        }
    }
}

混淆配置

參考資料

暫時空缺

項目demo下載地址

 https://github.com/haiyuKing/PreferencesUtilsDemo


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM