SharedPreferences工具类(2种)

2024-08-31 23:18
文章标签 工具 sharedpreferences

本文主要是介绍SharedPreferences工具类(2种),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



下载地址  点击打开链接

-----------------------------------------------------------------首先看方法1------------------------------------------------------------------------------

SharedPreferencesUtils1

package com.example.sharedpreferences;import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;public class SharedPreferencesUtils1 {private String name;private Context mContext;/*** 当instance==null时候,加同步锁* * @return*/private static SharedPreferencesUtils1 instance;public static SharedPreferencesUtils1 getInstance() {if (instance == null) {synchronized (SharedPreferencesUtils1.class) {if (instance == null) {instance = new SharedPreferencesUtils1();}}}return instance;}private SharedPreferencesUtils1() {}/*** 初始化上下文参数以及文件名* * @param context*            ,上下文* @param name*            ,文件名*/public void init(Context context, String name) {this.mContext = context.getApplicationContext();this.name = name;}/*** 保存数据,泛型方法* * @param key*            ,键值* @param value*            ,数据* @param <V>*/public <V> void setValue(@NonNull String key, V value) {SharedPreferences sp = mContext.getSharedPreferences(name,Context.MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();if (value instanceof String) {editor.putString(key, (String) value);} else if (value instanceof Integer) {editor.putInt(key, (Integer) value);} else if (value instanceof Long) {editor.putLong(key, (Long) value);} else if (value instanceof Boolean) {editor.putBoolean(key, (Boolean) value);} else if (value instanceof Float) {editor.putFloat(key, (Float) value);}editor.commit();}/*** 读取数据,泛型方法* * @param key*            ,键值* @param defaultValue*            ,默认值* @param <V>* @return*/public <V> V getValue(@NonNull String key, V defaultValue) {SharedPreferences sp = mContext.getSharedPreferences(name,Context.MODE_PRIVATE);Object value = defaultValue;if (defaultValue instanceof String) {value = sp.getString(key, (String) defaultValue);} else if (defaultValue instanceof Integer) {value = sp.getInt(key, (Integer) defaultValue);} else if (defaultValue instanceof Long) {value = sp.getLong(key, (Long) defaultValue);} else if (defaultValue instanceof Boolean) {value = sp.getBoolean(key, (Boolean) defaultValue);} else if (defaultValue instanceof Float) {value = sp.getFloat(key, (Float) defaultValue);}return (V) value;}/*** 清除数据*/public void clearData() {SharedPreferences.Editor editor = mContext.getSharedPreferences(name,Context.MODE_PRIVATE).edit();editor.clear();editor.commit();}
}

-----------------------------------------------------------------继续看方法2------------------------------------------------------------------------------

GDPreferenceSettings

package com.example.sharedpreferences;public enum GDPreferenceSettings {SETTING_IS_FIRST("com.godinsec.glauncher_isFirstIn", Boolean.TRUE);private final String mId;private final Object mDefaultValue;private GDPreferenceSettings(String id, Object defaultValue) {this.mId = id;this.mDefaultValue = defaultValue;}public String getId() {return this.mId;}public Object getDefaultValue() {return this.mDefaultValue;}}

GDPreferences

package com.example.sharedpreferences;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;public class GDPreferences {public static final String CCP_PREFERENCE = "geek2.xml";private static GDPreferences instance;public static GDPreferences getInstance() {if (instance == null) {synchronized (GDPreferences.class) {if (instance == null) {instance = new GDPreferences();}}}return instance;}private GDPreferences() {super();}public static SharedPreferences getSharedPreferences() {return MyApplication.getInstance().getSharedPreferences(CCP_PREFERENCE,Context.MODE_PRIVATE);}public static void savePreference(GDPreferenceSettings GDPreferenceSetting,Object value) {Map<GDPreferenceSettings, Object> prefs = new HashMap<GDPreferenceSettings, Object>();prefs.put(GDPreferenceSetting, value);savePreferences(prefs);}private static void savePreferences(Map<GDPreferenceSettings, Object> prefs) {SharedPreferences sp = getSharedPreferences();Editor editor = sp.edit();Iterator<GDPreferenceSettings> it = prefs.keySet().iterator();while (it.hasNext()) {GDPreferenceSettings pref = it.next();Object value = prefs.get(pref);if (value == null) {return;}if (value instanceof Boolean&& pref.getDefaultValue() instanceof Boolean) {editor.putBoolean(pref.getId(),((Boolean) value).booleanValue());} else if (value instanceof String&& pref.getDefaultValue() instanceof String) {editor.putString(pref.getId(), (String) value);} else if (value instanceof Integer&& pref.getDefaultValue() instanceof Integer) {editor.putInt(pref.getId(), (Integer) value);} else if (value instanceof Long&& pref.getDefaultValue() instanceof Long) {editor.putLong(pref.getId(), (Long) value);}}editor.commit();}
}

MyApplication

package com.example.sharedpreferences;import android.app.Application;public class MyApplication extends Application {private static MyApplication instance;@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();this.instance = this;}public static MyApplication getInstance(){return instance;}}

最后看测试代码

MainActivity

package com.example.sharedpreferences;import android.app.Activity;
import android.os.Bundle;
import android.text.StaticLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;public class MainActivity extends Activity {private static final String TAG = MainActivity.class.getSimpleName();@SuppressWarnings("static-access")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*** 方法一*/SharedPreferencesUtils1 sp = SharedPreferencesUtils1.getInstance();sp.init(this, "geek.xml");sp.<String>setValue("name", "geek");String value = sp.<String>getValue("name", "");Log.i(TAG, "value-->"+value);/*** 方法二*/GDPreferences.getInstance().savePreference(GDPreferenceSettings.SETTING_IS_FIRST, false);boolean SETTING_IS_FIRST = GDPreferences.getSharedPreferences().getBoolean(GDPreferenceSettings.SETTING_IS_FIRST.getId(), (Boolean) GDPreferenceSettings.SETTING_IS_FIRST.getDefaultValue());Log.i(TAG, "SETTING_IS_FIRST-->"+SETTING_IS_FIRST);}}



这篇关于SharedPreferences工具类(2种)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125274

相关文章

高效录音转文字:2024年四大工具精选!

在快节奏的工作生活中,能够快速将录音转换成文字是一项非常实用的能力。特别是在需要记录会议纪要、讲座内容或者是采访素材的时候,一款优秀的在线录音转文字工具能派上大用场。以下推荐几个好用的录音转文字工具! 365在线转文字 直达链接:https://www.pdf365.cn/ 365在线转文字是一款提供在线录音转文字服务的工具,它以其高效、便捷的特点受到用户的青睐。用户无需下载安装任何软件,只

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念

超强的截图工具:PixPin

你是否还在为寻找一款功能强大、操作简便的截图工具而烦恼?市面上那么多工具,常常让人无从选择。今天,想给大家安利一款神器——PixPin,一款真正解放双手的截图工具。 想象一下,你只需要按下快捷键就能轻松完成多种截图任务,还能快速编辑、标注甚至保存多种格式的图片。这款工具能满足这些需求吗? PixPin不仅支持全屏、窗口、区域截图等基础功能,它还可以进行延时截图,让你捕捉到每个关键画面。不仅如此

PR曲线——一个更敏感的性能评估工具

在不均衡数据集的情况下,精确率-召回率(Precision-Recall, PR)曲线是一种非常有用的工具,因为它提供了比传统的ROC曲线更准确的性能评估。以下是PR曲线在不均衡数据情况下的一些作用: 关注少数类:在不均衡数据集中,少数类的样本数量远少于多数类。PR曲线通过关注少数类(通常是正类)的性能来弥补这一点,因为它直接评估模型在识别正类方面的能力。 精确率与召回率的平衡:精确率(Pr

husky 工具配置代码检查工作流:提交代码至仓库前做代码检查

提示:这篇博客以我前两篇博客作为先修知识,请大家先去看看我前两篇博客 博客指路:前端 ESlint 代码规范及修复代码规范错误-CSDN博客前端 Vue3 项目开发—— ESLint & prettier 配置代码风格-CSDN博客 husky 工具配置代码检查工作流的作用 在工作中,我们经常需要将写好的代码提交至代码仓库 但是由于程序员疏忽而将不规范的代码提交至仓库,显然是不合理的 所

10个好用的AI写作工具【亲测免费】

1. 光速写作 传送入口:http://u3v.cn/6hXWYa AI打工神器,一键生成文章&ppt 2. 讯飞写作 传送入口:http://m6z.cn/5ODiSw 3. 讯飞绘文 传送入口:https://turbodesk.xfyun.cn/?channelid=gj3 4. AI排版助手 传送入口:http://m6z.cn/6ppnPn 5. Kim

分享5款免费录屏的工具,搞定网课不怕错过!

虽然现在学生们不怎么上网课, 但是对于上班族或者是没有办法到学校参加课程的人来说,网课还是很重要的,今天,我就来跟大家分享一下我用过的几款录屏软件=,看看它们在录制网课时的表现如何。 福昕录屏大师 网址:https://www.foxitsoftware.cn/REC/ 这款软件给我的第一印象就是界面简洁,操作起来很直观。它支持全屏录制,也支持区域录制,这对于我这种需要同时录制PPT和老师讲

生信圆桌x生信分析平台:助力生物信息学研究的综合工具

介绍 少走弯路,高效分析;了解生信云,访问 【生信圆桌x生信专用云服务器】 : www.tebteb.cc 生物信息学的迅速发展催生了众多生信分析平台,这些平台通过集成各种生物信息学工具和算法,极大地简化了数据处理和分析流程,使研究人员能够更高效地从海量生物数据中提取有价值的信息。这些平台通常具备友好的用户界面和强大的计算能力,支持不同类型的生物数据分析,如基因组、转录组、蛋白质组等。

IntelliJ IDEA - 强大的编程工具

哪个编程工具让你的工作效率翻倍? 在日益繁忙的工作环境中,选择合适的编程工具已成为提升开发者工作效率的关键。不同的工具能够帮助我们简化代码编写、自动化任务、提升调试速度,甚至让团队协作更加顺畅。那么,哪款编程工具让你的工作效率翻倍?是智能的代码编辑器,强大的版本控制工具,还是那些让你事半功倍的自动化脚本?在这里我推荐一款好用的编程工具:IntelliJ IDEA。 方向一:工具介绍 Int

BIRT--商业智能和报表工具,从零开始

1.简介 BIRT (Business Intelligence and Reporting Tools), 是为 Web 应用程序开发的基于 Eclipse 的开源报表系统,特别之处在于它是以 Java 和 JavaEE 为基础。BIRT 有两个主要组件:基于 Eclipse 的报表设计器,以及部署到应用服务器上的运行时组件。 2.下载 官网下载网址:http://download.ec