1. 程式人生 > >【Android】一個好用的sharedpreferences存儲類方法

【Android】一個好用的sharedpreferences存儲類方法

true spa Edito roi util https pack fig rda

其實我也不知道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 * 郵箱:[email protected] * 不會寫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(); } }

其實如果你不是存儲的map類型數據,就不需要這麽麻煩了,直接put就完事了。。。

下面給出sharedpreferences的一般用法:

參考這篇文章即可

【Android】一個好用的sharedpreferences存儲類方法