分享一個SharedPreferences的工具類,方便儲存資料

來源:互聯網
上載者:User

我們平常儲存一些資料,都會用到SharedPreferences,他是儲存在手機裡面的,具體路徑是data/data/你的包名/shared_prefs/儲存的檔案名稱.xml, SharedPreferences的使用也很簡單,我自己就寫了一個SharedPreferences的工具類,然後就儲存在這裡,等自己以後需要儲存資料直接從這裡copy代碼,哈哈

工具類如下

package com.example.shortcut;import android.content.Context;import android.content.SharedPreferences;/** * SharedPreferences的一個工具類,調用setParam就能儲存String, Integer, Boolean, Float, Long類型的參數 * 同樣調用getParam就能擷取到儲存在手機裡面的資料 * @author xiaanming * */public class SharedPreferencesUtils {/** * 儲存在手機裡面的檔案名稱 */private static final String FILE_NAME = "share_date";/** * 儲存資料的方法,我們需要拿到儲存資料的具體類型,然後根據類型調用不同的儲存方法 * @param context * @param key * @param object  */public static void setParam(Context context , String key, Object object){String type = object.getClass().getSimpleName();SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();if("String".equals(type)){editor.putString(key, (String)object);}else if("Integer".equals(type)){editor.putInt(key, (Integer)object);}else if("Boolean".equals(type)){editor.putBoolean(key, (Boolean)object);}else if("Float".equals(type)){editor.putFloat(type, (Float)object);}else if("Long".equals(type)){editor.putLong(type, (Long)object);}editor.commit();}/** * 得到儲存資料的方法,我們根據預設值得到儲存的資料的具體類型,然後調用相對於的方法擷取值 * @param context * @param key * @param defaultObject * @return */public static Object getParam(Context context , String key, Object defaultObject){String type = defaultObject.getClass().getSimpleName();SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);if("String".equals(type)){return sp.getString(key, (String)defaultObject);}else if("Integer".equals(type)){return sp.getInt(key, (Integer)defaultObject);}else if("Boolean".equals(type)){return sp.getBoolean(key, (Boolean)defaultObject);}else if("Float".equals(type)){return sp.getFloat(type, (Float)defaultObject);}else if("Long".equals(type)){return sp.getLong(type, (Long)defaultObject);}return null;}}

使用也很簡單,儲存資料

SharedPreferencesUtils.setParam(this, "String", "xiaanming");SharedPreferencesUtils.setParam(this, "int", 10);SharedPreferencesUtils.setParam(this, "boolean", true);SharedPreferencesUtils.setParam(this, "long", 100L);SharedPreferencesUtils.setParam(this, "float", 1.1f);

擷取資料

SharedPreferencesUtils.getParam(TimerActivity.this, "String", "");                                                                                        SharedPreferencesUtils.getParam(TimerActivity.this, "int", 0);SharedPreferencesUtils.getParam(TimerActivity.this, "boolean", false);SharedPreferencesUtils.getParam(TimerActivity.this, "long", 0L);SharedPreferencesUtils.getParam(TimerActivity.this, "float", 0.0f);

是不是挺方便的呢,希望對看過文章的你有一點點的協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.