2016-12-23 122 views
0

我做一個sharedpreference,但每次我把它的時間,它只是不更新​​值SharedPreferences值不更新

package com.example.david.tesztapp; 

import java.util.HashMap; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.provider.SyncStateContract; 
import android.util.Log; 

public class SessionManagement { 

// Shared Preferences 
SharedPreferences pref; 

// Editor for Shared preferences 
Editor editor; 

// Context 
Context _context; 

// Shared pref mode 
int PRIVATE_MODE = 0; 


private static SessionManagement sInstance; 

// Sharedpref file name 
private static final String PREF_NAME = "LoginSession"; 

// All Shared Preferences Keys 
public static final String IS_LOGIN = "IsLoggedIn"; 

// User name (make variable public to access from outside) 
public static final String KEY_NAME = "name"; 

// Email address (make variable public to access from outside) 
//public static final String KEY_EMAIL = "email"; 

// Constructor 
public SessionManagement(Context context){ 
    this._context = context; 
    pref = _context.getSharedPreferences(PREF_NAME,PRIVATE_MODE); 
    editor = pref.edit(); 
    //editor.clear(); 
} 

public static synchronized void initializeInstance(Context context) { 
    if (sInstance == null) { 
     sInstance = new SessionManagement(context); 
    } 
} 

public static synchronized SessionManagement getInstance() { 
    if (sInstance == null) { 
     throw new IllegalStateException(SessionManagement.class.getSimpleName() + 
       " is not initialized, call initializeInstance(..) method first."); 
    } 
    return sInstance; 
} 
/** 
* Create login session 
* */ 
public void createLoginSession(String name){ 

    editor.clear(); 
    editor.apply(); 
    // Storing login value as TRUE 

    editor.putBoolean(IS_LOGIN, true); 

    // Storing name in pref 
    editor.putString(KEY_NAME, name); 

    // Storing email in pref 
    // editor.putString(KEY_EMAIL, email); 

    // commit changes 
    editor.apply(); 

    Log.i("Editor",""+IS_LOGIN); 
} 

/** 
* Check login method wil check user login status 
* If false it will redirect user to login page 
* Else won't do anything 
* */ 
public void checkLogin(){ 
    // Check login status 
    if(!this.isLoggedIn()){ 
     // user is not logged in redirect him to Login Activity 
     Intent i = new Intent(_context, LoginActivity.class); 
     // Closing all the Activities 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     // Add new Flag to start new Activity 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     // Staring Login Activity 
     _context.startActivity(i); 
    } 


} 



/** 
* Get stored session data 
* */ 
public HashMap<String, String> getUserDetails(){ 
    HashMap<String, String> user = new HashMap<String, String>(); 
    // user name 
    user.put(KEY_NAME, pref.getString(KEY_NAME, null)); 

    // user email id 
    // user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null)); 

    // return user 
    return user; 
} 

/** 
* Clear session details 
* */ 
public void logoutUser(){ 
    // Clearing all data from Shared Preferences 
    editor.clear(); 
    editor.commit(); 

    // After logout redirect user to Loing Activity 
    Intent i = new Intent(_context, LoginActivity.class); 
    // Closing all the Activities 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    // Add new Flag to start new Activity 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    // Staring Login Activity 
    _context.startActivity(i); 
} 

/** 
* Quick check for login 
* **/ 
// Get Login State 
public boolean isLoggedIn(){ 
    return pref.getBoolean(IS_LOGIN, false); 

} 
} 

,然後我把它在的AsyncTask

@Override 
    protected void onPostExecute(String result) { 

     //this method will be running on UI thread 
     pdLoading.dismiss(); 

     Log.i("Result "," "+result); 



     if(result.equalsIgnoreCase("true")){ 
      /* Here launching another activity when login successful. If you persist login state 
      use sharedPreferences of Android. and logout button to clear sharedPreferences. 
      */ 
      Log.i("User "," correct "); 

      session.createLoginSession("USER"); 


      Intent intent = new Intent(getApplicationContext(),SuccessActivity.class); 
      startActivity(intent); 
      finish(); 


     }else if (result.equalsIgnoreCase("false")){ 

      // If username and password does not match display a error message 
      Toast toast = Toast.makeText(LoginActivity.this, "Helytelen felhasználónév vagy jelszó", Toast.LENGTH_LONG); 
      toast.show(); 

     } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) { 

      Toast toast = Toast.makeText(LoginActivity.this, "Csatlakozási hiba történt!", Toast.LENGTH_LONG); 
      toast.show(); 

     } 

     Log.i("Sess ",""+session.IS_LOGIN); 

,並添加到了在OnCreate

SessionManagement session = new SessionManagement(getApplicationContext());

在DDMS

我看到塔爾sharedpref erence被創建,但它只是不更新​​值

回答

0

你可以試試這個

public void createLoginSession(String name){ 
    editor = pref.edit(); 
    editor.putBoolean(IS_LOGIN, true); 
    editor.putString(KEY_NAME, name); 
    // commit change 
    editor.commit(); // replace commit with apply 
    Log.i("Editor",""+IS_LOGIN); 
} 

更新

使用應用程序

public class ApplicationBase extends Application { 

    public static SharedPreferences mSharedPreferences; 
    public static SharedPreferences.Editor editor; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mSharedPreferences = getSharedPreferences("MYPREF", MODE_PRIVATE); 
     editor = mSharedPreferences.edit(); 
    } 

    public static SharedPreferences getmSharedPreferences() { 
     return mSharedPreferences; 
    } 

    public static void setmSharedPreferences(SharedPreferences mSharedPreferences) { 
     ApplicationBase.mSharedPreferences = mSharedPreferences; 
    } 

    public static SharedPreferences.Editor getEditor() { 
     return editor; 
    } 

    public static void setEditor(SharedPreferences.Editor editor) { 
     ApplicationBase.editor = editor; 
    } 
} 

在應用類中定義這個

// All Shared Preferences Keys 
public static final String IS_LOGIN = "IsLoggedIn"; 

// User name (make variable public to access from outside) 
public static final String KEY_NAME = "name"; 

添加像這樣

ApplicationBase.getEditor().putBoolean(IS_LOGIN,true).commit(); 
ApplicationBase.getEditor().putString(KEY_NAME, name).commit(); 

得到這樣

ApplicationBase.getmSharedPreferences().getString(KEY_NAME, ""); 
ApplicationBase.getmSharedPreferences().getBoolean(IS_LOGIN, false); 
+0

遺憾的是仍然沒有工作 –

+0

它給回起點結果爲IS_LOGIN '/結果:爲真 /用戶:正確 /編輯:IsLoggedIn' –

+0

@davidimre,c你請檢查日誌?檢查是否有一些異常或警告正在拋出。 –

0

做到這一點的變化,讓我知道它的工作原理或不..

public void createLoginSession(String name) 
{ 
editor.putBoolean(IS_LOGIN, true); 
editor.putString(KEY_NAME, name); 
editor.apply(); 
Log.i("Editor",""+IS_LOGIN); 
} 
+0

仍然沒有工作:( –

+0

我已檢查您的會話類,我發現,它是保存鍵值對。當我登錄時,它總是給我敬酒「用戶登錄狀態:真」,當我做註銷時,它顯示錯誤的結果 –

+0

但有時當我登錄它仍然給「用戶登錄狀態:false」 我該如何擺脫? –