其实我也不知道sharedpreferences究竟安全不安全,毕竟是android中最简单的存储机制。

如果你手机root了的话,使用MT管理器到data/data/包名/shared_prefs下就可以找到这个xml文件,而且你可以更改它的内容。

所以一般不推荐使用这种方法来存储一些比较重要的信息(密码、个人信息等等)。

因此该类只是用作演示,后期考虑使用Base64对重要信息进行加密处理。

以下是代码:

package com.paul.notebook.dataBase;

import android.content.Context;
import android.content.SearchRecentSuggestionsProvider;
import android.content.SharedPreferences; import com.paul.notebook.LoginActivity; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; /**
* 作者:created by 巴塞罗那的余晖 on 2019/3/10 18:45
* 邮箱:zhubaoluo@outlook.com
* 不会写BUG的程序猿不是好程序猿,嘤嘤嘤
*/
public class userData {
private Map<String,String> root;//用户数据根目录
private static String flag="user_data";//标识
private Context mcontext;
public userData(Context context)
{
root=new HashMap<>();
mcontext=context;
readData(flag);
}
public boolean findExistUsername(String name)//查找是否有该用户名
{
return root.containsKey(name);
}
public boolean verifyPassword(String name,String password)//验证用户名和密码是否匹配
{
if(findExistUsername(name))
{
if(root.get(name).equals(password))
{
return true;
}
}
return false;
}
public boolean getRegister(String name,String password)//注册,并返回是否注册成功
{
if(findExistUsername(name))
{
return false;
}
root.put(name,password);
saveData(flag);
return true;
}
private void saveData(String key)//保存数据到本地
{
//原理:将Map格式转换成json字符串,并指定一个特定标识来记录,Value按照发生器逐一保存就好
JSONArray mJsonArray = new JSONArray();
Iterator<Map.Entry<String, String>> iterator = root.entrySet().iterator();
JSONObject object = new JSONObject(); while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
try {
object.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
//异常处理
//妈咪妈咪哄!BUG快离开!
}
}
mJsonArray.put(object);
SharedPreferences sp=mcontext.getSharedPreferences("config",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString(key,mJsonArray.toString());
editor.commit();
}
private void readData(String key)//sharedpreferences从本地读取数据
{
root.clear();
SharedPreferences sp=mcontext.getSharedPreferences("config",Context.MODE_PRIVATE);
String result=sp.getString(key,"");
try {
JSONArray array=new JSONArray(result);
for(int i=0;i<array.length();i++)
{
JSONObject itemObject =array.getJSONObject(i);
JSONArray names=itemObject.names();
if(names!=null)
{
for(int j=0;j<names.length();j++)
{
String name=names.getString(j);
String value=itemObject.getString(name);
root.put(name,value);
}
}
}
}catch (JSONException e){
//异常处理
//妈咪妈咪哄!BUG快离开!
}
}
private void clearLocalData()//清除本地数据,这个方法一般用不到,故写成私有的
{
SharedPreferences preferences = mcontext.getSharedPreferences("config", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit(); } }

使用方法

data=new userData(LoginActivity.this);//这里只用传递当前活动,文件名之类的都已经在类体中定义过,可以按需修改

其实如果你不是存储的map类型数据,就不需要这么麻烦了,直接put就完事了。。。

下面给出sharedpreferences的一般用法:

参考这篇文章即可(简书是个好东西)

这里我用SharedPreferences写了一个记事本存储的类,可以做一个大体的参考,功能基本通用。

package com.paul.notebook.dataBase;

import android.content.Context;
import android.content.SharedPreferences; import com.bumptech.glide.disklrucache.DiskLruCache; /**
* 作者:created by 巴塞罗那的余晖 on 2019/3/10 22:20
* 邮箱:zhubaoluo@outlook.com
* 不会写BUG的程序猿不是好程序猿,嘤嘤嘤
*/
public class contentData {
private String content;//记事本内容
private String title;//记事本标题,作为标识符
private Context context;//传入的活动
private String flag;//对应标题的key public void setContent(String content) {
this.content = content;
}
public void setTitle(String title)
{
this.title=title;
}
public void put(String title,String content)
{
this.title=title;
this.content=content;
}
public contentData(Context context,String flag)//初始化函数,传入当前活动和key
{
this.context=context;
content="";
title="";
this.flag=flag;
}
private void save_data()//保存数据到本地
{
SharedPreferences sp=context.getSharedPreferences("content_data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString(title,content);//标题对应文章内容
editor.putString(flag,title);//flag对应标题
editor.commit();//提交保存
}
public boolean readData()
{
boolean result=true;
SharedPreferences sp=context.getSharedPreferences("content_data", Context.MODE_PRIVATE);
title=sp.getString(flag,"哦吼");//先取出标题,如果没有找到就返回后面的值
if(title.equals("哦吼"))
{
result=false;
}
else
{
content = sp.getString(title, "");//取出title后再去取对应的内容
if(content.equals(""))
{
result=false;
} }
return result; } public boolean saveData()//保证标题和内容都不为空(空也是可以存储的,但是没意义)
{
if(title==""||content=="")
return false;
save_data();
return true;
} public String getContent() {
return content;
} public String getTitle() {
return title;
}
}

使用方法,类似于前面的Map

data=new contentData(MainActivity.this,username);//传入当前活动和你要保存的key,保存文件名称是默认的

【Android】一个好用的sharedpreferences存储类方法的更多相关文章

  1. Android学习笔记_8_使用SharedPreferences存储数据

    1.SharedPreferences介绍: Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPrefer ...

  2. Android数据存储之SharedPreferences存储

    安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布 ...

  3. Android SharedPreferences存储

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_SharedPreferences.html 一 概念 SharedPreferen ...

  4. Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  5. Android入门(九)文件存储与SharedPreferences存储

    原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...

  6. 【Android】数据的应用-使用sharedpreferences存储数据

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  7. 【Mark】Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  8. Android SharedPreferences存储详解

    什么是SharedPreferences存储 一种轻量级的数据保存方式 类似于我们常用的ini文件,用来保存应用程序的一些属性设置.较简单的参数设置. 保存现场:保存用户所作的修改或者自定义参数设定, ...

  9. Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

随机推荐

  1. iOS开发- SceneKit

    打开你的Xcode 6然后新建一个项目,选择iOS/Application/Game模板然后点击Next. 将项目命名为QuickStart,选择开发语言为Swift,然后游戏选用的平台技术选择为Sc ...

  2. Recommendation system

    Dear Prof.Choi: My research interest is mainly the application and optimization of big data and arti ...

  3. B1090 [SCOI2003]字符串折叠 区间dp

    又一道区间dp,和上一篇类似,但是比他简单,这个只有两种转移方法,不是很复杂.直接判断是否为重复的串就行. 题干: Description 折叠的定义如下: . 一个字符串可以看成它自身的折叠.记作S ...

  4. bzoj 2333 [SCOI2011]棘手的操作 —— 可并堆

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2333 稍微复杂,参考了博客:http://hzwer.com/5780.html 用 set ...

  5. 在LNMP或Nginx上配置NameCheap免费SSL证书

  6. Gym - 101982B 2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) B. Coprime Integers Mobius+容斥 ab间gcd(x,y)=1的对数

    题面 题意:给你 abcd(1e7),求a<=x<=b,c<=y<=d的,gcd(x,y)=1的数量 题解:经典题目,求从1的到n中选x,从1到m中选y的,gcd(x,y)=k ...

  7. Akka源码分析-ask模式

    在我之前的博文中,已经介绍过要慎用Actor的ask.这里我们要分析一下ask的源码,看看它究竟是怎么实现的. 开发时,如果要使用ask方法,必须要引入akka.pattern._,这样才能使用ask ...

  8. 关于swoole 和golang 的压力测试结果

    一.环境介绍 linux centos7 php7.1.18 go1.12.1 2核4G内存 二.代码 swoole代码 <?php $http = new swoole_http_server ...

  9. 题解报告:hdu 1285 确定比赛名次

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...

  10. 新认知之WinForm窗体程序

    Windows应用程序和控制台应用程序有很大的区别 >Form1.cs  :窗体文件,程序员对窗体编写的代码一般都存放在这个文件中. >Form1.Designer.cs :窗体设计文件, ...