2017-10-21 244 views
0

我已經創建了一個用戶登錄應用程序。登錄後,它將所有檢索到的數據存儲到我的sharedPrefManager,但之後我希望它每次重新啓動應用程序或更新我的用戶詳細信息時檢查更新並獲取更改的數據,或者每次打開profile_Activity(Specific)時刷新一次。如何在Android中更新SharedPrefManager

這裏是我的SharedPref

public class SharedPrefManager { 

    private static SharedPrefManager mInstance; 
    private static Context mCtx; 
    private static final String SHARED_PREF_NAME = "mysharedpref12"; 
    private static final String KEY_USERNAME = "username"; 
    private static final String KEY_USER_MAIL = "usermail"; 
    private static final String KEY_USER_ID = "userid"; 
    private static final String KEY_PHONE = "userphone"; 
    private static final String KEY_NAME = "usename"; 
    private static final String KEY_PIN = "pin"; 
    private static final String KEY_SUR = "surname"; 
    private static final String KEY_ADD = "address"; 

    private SharedPrefManager(Context context) { 
     mCtx = context; 
    } 

    public static synchronized SharedPrefManager getInstance(Context context) { 
     if (mInstance == null) { 
      mInstance = new SharedPrefManager(context); 
     } 
     return mInstance; 
    } 

    public boolean userLogin(int id, int pin, String phone, String username, String mail, String name, String surname, String address/**, String catagory*/){ 

     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 

     editor.putInt(KEY_USER_ID, id); 
     editor.putInt(KEY_PIN, pin); 
     editor.putString(KEY_PHONE, phone); 
     editor.putString(KEY_USERNAME, username); 
     editor.putString(KEY_USER_MAIL, mail); 
     editor.putString(KEY_NAME, name); 
     editor.putString(KEY_SUR, surname); 
     editor.putString(KEY_ADD, address); 
     //editor.putString(KEY_CATA, catagory); 

     editor.apply(); 

     return true; 

    } 

    public boolean isLoggedIn(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     if (sharedPreferences.getString(KEY_USERNAME, null) != null){ 
      return true; 
     } 
      return false; 
    } 

    public boolean logOut(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.clear(); 
     editor.apply(); 
     return true; 
    } 

    public int getid(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getInt(KEY_USER_ID, 0); 
     //return sharedPreferences.getInt(KEY_USER_ID, Integer.parseInt(null)); 
    } 

    public int getpin(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getInt(KEY_PIN,0); 
     /*SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getInt(KEY_PIN, Integer.parseInt(null));*/ 
    } 

    public String getUsername(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY_USERNAME, null); 
    } 

    public String getUserEmail(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY_USER_MAIL, null); 
    } 

    public String getUserPhone(){ 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY_PHONE, null); 
    } 

    public String getName() { 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY_NAME, null); 
    } 

    public String getSurname() { 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY_SUR, null); 
    } 

    public String getAddress() { 
     SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.getString(KEY_ADD, null); 
    } 

} 

回答

0

我有一個sharedPreference類saperate -

1> AppPreference.java

public class AppPreference { 


    public static final String PREF_USERID = "prefUserId"; 
    public static final String PREF_FNAME = "prefFname"; 
    public static final String PREF_LNAME = "prefLname"; 
    public static final String PREF_PHONE = "prefphone"; 


    public static final void setStringPref(Context context, String prefKey, String key, String value) { 
     SharedPreferences sp = context.getSharedPreferences(prefKey, 0); 
     SharedPreferences.Editor edit = sp.edit(); 
     edit.putString(key, value); 
     edit.commit(); 
    } 

    public static final String getStringPref(Context context, String prefName, String key) { 
     SharedPreferences sp = context.getSharedPreferences(prefName, 0); 
     return sp.getString(key, ""); 
    } 

    public static final class PREF_KEY { 
     public static final String USERID = "userid"; 
     public static final String FNAME = "fname"; 
     public static final String LNAME = "lname"; 
     public static final String PHONE = "phone"; 
    } 
} 

2> LoginActivity.java

if (jsonObject.toString().contains(Constant.JSON_KEY.RESPONSE)) { 
         JSONObject response = jsonObject.getJSONObject(Constant.JSON_KEY.RESPONSE); 

         String userId = response.getString(Constant.JSON_KEY.ID); 
         String fname = response.getString(Constant.JSON_KEY.FNAME); 
         String lname = response.getString(Constant.JSON_KEY.LNAME); 
         String phone = response.getString(Constant.JSON_KEY.PHONE); 

         SignUpDetailsClass signUpDetailsClass = new SignUpDetailsClass(); 
         signUpDetailsClass.setId(userId); 
         signUpDetailsClass.setFname(fname); 
         signUpDetailsClass.setLname(lname); 
         signUpDetailsClass.setPhone(phone); 

         **// store data in sharedPreference** 
         AppPreference.setStringPref(context, AppPreference.PREF_USERID, AppPreference.PREF_KEY.USERID, userId); 
         AppPreference.setStringPref(context, AppPreference.PREF_FNAME, AppPreference.PREF_KEY.FNAME, fname); 
         AppPreference.setStringPref(context, AppPreference.PREF_LNAME, AppPreference.PREF_KEY.LNAME, lname); 
         AppPreference.setStringPref(context, AppPreference.PREF_PHONE, AppPreference.PREF_KEY.PHONE, phone); 
    } 

3> Profile.java

     **// get data from sharedPreference** 
         String userId = AppPreference.getStringPref(context, AppPreference.PREF_USERID, AppPreference.PREF_KEY.USERID); 
         String strMob = AppPreference.getStringPref(context, AppPreference.PREF_PHONE, AppPreference.PREF_KEY.PHONE); 
         String strFirstName = AppPreference.getStringPref(context, AppPreference.PREF_FNAME, AppPreference.PREF_KEY.FNAME); 
         String strLastName = AppPreference.getStringPref(context, AppPreference.PREF_LNAME, AppPreference.PREF_KEY.LNAME); 

在這裏,在第一類爲AppPreference.java另一類爲Login.java在偏好設置所有數據,然後你可以在你的應用程序的任何地方獲取數據,例如見得到所有數據在Profile.java

希望這可以幫助你。

+0

因此,您是說每次重新啓動應用程序時,都使用AppPreference.java中存儲的密鑰進行登錄?並更新profile.java中的鍵值? –

+0

是的,您可以通過API響應設置SharedPreference中的所有字段數據,並將它們分配到您的應用程序中,這通過使用此類很簡單,只需調用設置數據的「setStringPref」和獲取數據的「getStringPref」即可。 如果你需要每次應用程序重新啓動,那麼你需要從飛濺API得到daat。 希望你明白我對你說的話,這對你有所幫助。 – InsaneCat