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

相关文章

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

MySQL慢查询工具的使用小结

《MySQL慢查询工具的使用小结》使用MySQL的慢查询工具可以帮助开发者识别和优化性能不佳的SQL查询,本文就来介绍一下MySQL的慢查询工具,具有一定的参考价值,感兴趣的可以了解一下... 目录一、启用慢查询日志1.1 编辑mysql配置文件1.2 重启MySQL服务二、配置动态参数(可选)三、分析慢查

基于Python实现进阶版PDF合并/拆分工具

《基于Python实现进阶版PDF合并/拆分工具》在数字化时代,PDF文件已成为日常工作和学习中不可或缺的一部分,本文将详细介绍一款简单易用的PDF工具,帮助用户轻松完成PDF文件的合并与拆分操作... 目录工具概述环境准备界面说明合并PDF文件拆分PDF文件高级技巧常见问题完整源代码总结在数字化时代,PD

Python按照24个实用大方向精选的上千种工具库汇总整理

《Python按照24个实用大方向精选的上千种工具库汇总整理》本文整理了Python生态中近千个库,涵盖数据处理、图像处理、网络开发、Web框架、人工智能、科学计算、GUI工具、测试框架、环境管理等多... 目录1、数据处理文本处理特殊文本处理html/XML 解析文件处理配置文件处理文档相关日志管理日期和

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

基于Python实现简易视频剪辑工具

《基于Python实现简易视频剪辑工具》这篇文章主要为大家详细介绍了如何用Python打造一个功能完备的简易视频剪辑工具,包括视频文件导入与格式转换,基础剪辑操作,音频处理等功能,感兴趣的小伙伴可以了... 目录一、技术选型与环境搭建二、核心功能模块实现1. 视频基础操作2. 音频处理3. 特效与转场三、高

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核