zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    HDOJ1556 Color the ball
    BUPT( 北邮)2 大数 AB
    DateTime.ToString()的用法
    判断DataTable中的空值(字段为数值型)?
    [转].NET平台下的Excel编程|C#操作Excel|Application和ApplicationClass的联系和区别
    [转]寻找SqlHelper
    C#认识/理解/运用 StreamReader,StreamWriter,StringReader,StringWriter[转]
    C# 应用微软的Visual Studio International Pack 类库提取汉字拼音首字母[转]
    [转]想靠写程序赚更多钱,写到两眼通红,写得比别人都又快又好好几倍,结果又能如何?
    DataRow复制一行到另一个DataTable[转]
  • 原文地址:https://www.cnblogs.com/whycxb/p/7026666.html
Copyright © 2011-2022 走看看