2016-09-19 98 views
0

我正在學習android並試圖找出執行共享首選項類的最佳方法。這是sharedPreference類的一個例子;SharedPreference:如何創建sharedPreference類

public static final String MyPREFERENCES = "MyPrefs" ; 
    public static final String Name = "nameKey"; 
    public static final String Phone = "phoneKey"; 
    public static final String Email = "emailKey"; 
    SharedPreferences sharedpreferences; 

我想把它變成類似於類似這樣的類。

package pesa.sharedpreferencedemo.Utils; 

import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.preference.PreferenceManager; 

/** 
* Created by mpan0590 on 9/19/2016. 
*/ 

public class SharedPreference { 

     public static final String PREFS_NAME = "PESASEND_PREFS"; 
     public static final String PREFS_KEY = "AOP_PREFS_String"; 


     public SharedPreference() { 
      super(); 
     } 

     public void save(Context context, String text) { 
      SharedPreferences settings; 
      Editor editor; 

      //settings = PreferenceManager.getDefaultSharedPreferences(context); 
      settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1 
      editor = settings.edit(); //2 

      editor.putString(PREFS_KEY, text); //3 

      editor.commit(); //4 
     } 

     public String getValue(Context context) { 
      SharedPreferences settings; 
      String text; 

      //settings = PreferenceManager.getDefaultSharedPreferences(context); 
      settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
      text = settings.getString(PREFS_KEY, null); 
      return text; 
     } 

     public void clearSharedPreference(Context context) { 
      SharedPreferences settings; 
      Editor editor; 

      //settings = PreferenceManager.getDefaultSharedPreferences(context); 
      settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
      editor = settings.edit(); 

      editor.clear(); 
      editor.commit(); 
     } 

     public void removeValue(Context context) { 
      SharedPreferences settings; 
      Editor editor; 

      settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 
      editor = settings.edit(); 

      editor.remove(PREFS_KEY); 
      editor.commit(); 
     } 
} 

有沒有辦法,我可以爲上面的例子中做的一樣,我是新來這個sharedpreference/Android的東西,它是一個有點混亂。我基本上只是想創建一個sharedpreference類,您可以在其中添加新值,編輯當前值和刪除其他值。我在這裏展示的代碼不是我的,而是來自我在網上找到的教程。

+1

教程中的代碼有什麼問題? –

+0

你不需要一個類來管理你的SharedPreferences,只需要使用它就更方便 –

+0

檢查它。 –

回答

0

您可以使類這樣

public class SharedPrefManager { 

    public static SharedPreferences getSharedPref(Context mContext) { 
     SharedPreferences pref = mContext.getSharedPreferences(Constants.SETTINGS, Context.MODE_PRIVATE); 

     return pref; 
    } 

    public static void setPrefVal(Context mContext, String key, String value) { 
     if(key!=null){ 
     Editor edit = getSharedPref(mContext).edit(); 
     edit.putString(key, value); 
     edit.commit(); 
     } 
    } 

    public static void setIntPrefVal(Context mContext, String key, int value) { 
     if(key!=null){ 
      Editor edit = getSharedPref(mContext).edit(); 
      edit.putInt(key, value); 
      edit.commit(); 
     } 
    } 

    public static void setLongPrefVal(Context mContext, String key, Long value) { 
     if(key!=null){ 
      Editor edit = getSharedPref(mContext).edit(); 
      edit.putLong(key, value); 
      edit.commit(); 
     } 
    } 

    public static void setBooleanPrefVal(Context mContext, String key, boolean value) { 
     if(key!=null){ 
      Editor edit = getSharedPref(mContext).edit(); 
      edit.putBoolean(key, value); 
      edit.commit(); 
     } 
    } 


    public static String getPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     String val = ""; 
     try { 
      if (pref.contains(key)) 
       val = pref.getString(key, ""); 
      else 
       val = ""; 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return val; 
    } 

    public static int getIntPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     int val = 0; 
     try { 
     if(pref.contains(key)) val = pref.getInt(key, 0); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return val; 
    } 

    public static Long getLongPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     Long val = null; 
     try{ 
     if(pref.contains(key)) val = pref.getLong(key, 0); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
     return val; 
    } 

    public static boolean getBooleanPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     boolean val = false; 
     try{ 
     if(pref.contains(key)) val = pref.getBoolean(key, false); 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return val; 
    } 


    public static boolean containkey(Context mContext,String key) 
    { 
     SharedPreferences pref = getSharedPref(mContext); 
     return pref.contains(key); 
    } 

    public static void saveTestScreens(Context mContext, String key, 
      String value) { 
     Editor edit = getSharedPref(mContext).edit(); 
     edit.putString(key, value); 
     edit.commit(); 
    } 

    public static String getTestScreens(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     String val = ""; 
     if (pref.contains(key)) 
      val = pref.getString(key, ""); 
     else 
      val = ""; 
     return val; 
    } 
} 
+0

我要試一試 – mpanga

+0

@mpanga okk讓我知道進一步的幫助,如果它可以幫助你達到一定程度,那麼請接受它作爲答案和upvote –

0

這是一個很好看,你正試圖在SharedPreferences創建一個抽象層,它簡化整個代碼庫,使之更具可讀性。但是不要自己編寫每個實用程序層,您可以嘗試使用現有的庫或代碼庫來避免重新發明輪子並調整代碼重用原則。只需專注於爲自己的應用程序編寫核心業務邏輯。

有許多圖書館,在github那裏你可以很容易地使用。 TinyDB就是這樣一個用於SharedPreferences抽象層,我碰巧使用它,迄今爲止它工作得非常好。如果您想要更高級的功能,請嘗試在SharedPreferences上搜索基於RxJava的抽象層,我相信您會發現很多。

+1

調查TinyDb – mpanga