我做一個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());
我看到塔爾sharedpref erence被創建,但它只是不更新值
遺憾的是仍然沒有工作 –
它給回起點結果爲IS_LOGIN '/結果:爲真 /用戶:正確 /編輯:IsLoggedIn' –
@davidimre,c你請檢查日誌?檢查是否有一些異常或警告正在拋出。 –