我将我的应用的偏好设置存储在共享预防中。
问题在于,当我尝试获取存储的首选项时,总是得到相同的值,具体而言,我在创建“会话”时存储的最后一个值
这是单击按钮存储首选项时的代码
//BUTTON TO STORE PREFERENCES IF THE CONNECTION TO FTP IS SUCCEFULLY
final TextView guardarconexionconftp= (TextView)findViewById(R.id.label_guardar_conexionconftp);
guardarconexionconftp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
FTPHandler ftp = new FTPHandler(ConfigActivity.this);
TextView dirftp= (TextView)findViewById(R.id.dirftp);
TextView userftp= (TextView)findViewById(R.id.userftp);
TextView passftp= (TextView)findViewById(R.id.passftp);
TextView logoutconexionconftp= (TextView)findViewById(R.id.label_guardar_conexionconftp);
String[] ftpurl = dirftp.getText().toString().split(":"); String ftpuser = userftp.getText().toString(); String ftppass = passftp.getText().toString();
if(ftpurl.length < 2) System.out.println("Direccion");
else if(ftpuser.length() == 0) System.out.println("User");
else if(ftppass.length() == 0) System.out.println("Pass");
else{
if(ftp.ftpConnect(ftpurl[0], ftpuser, ftppass, Integer.parseInt(ftpurl[1]))){
//STORE PREFERENCES
SessionManager session2;
session2 = new SessionManager(ConfigActivity.this);
System.out.println(ftpurl[0].toString() + " " + ftpuser.toString() + " " + ftppass.toString() + " " + ftpurl[1].toString());
session2.createFTPLoginSession(ftpurl[0].toString(), ftpuser.toString(),ftppass.toString(),ftpurl[1].toString());
guardarconexionconftp.setVisibility(View.GONE);
logoutconexionconftp.setVisibility(View.VISIBLE);
findViewById(R.id.camposconexion).setVisibility(View.VISIBLE);
editftp.setText("Conectado con a FTP");
findViewById(R.id.camposftp).setVisibility(View.GONE);
System.out.println("Disconnect " + ftp.ftpDisconnect());
System.out.println("print stored: " + session2.getFTPUrl() + " " + session2.getFTPUser() + " " + session2.getFTPpass() + " " + session2.getFTPPort());
}
else Toast.makeText(ConfigActivity.this, "No ha sido posible conectarse", Toast.LENGTH_SHORT).show();
}
}
});
这是存储和读取共享偏好的类
package com.asde.telemedicina;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Toast;
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "TelemedicinaPref";
// All Shared Preferences Keys
private static final String IS_LOGIN = "false";
private static final String IS_LOGINFTP = "false";
private static final String KEY_NOMBRE = "name";
private static final String KEY_REGISTRO = "regcode";
private static final String KEY_COLEGIADO = "colecode";
private static final String KEY_IDIOMA = "ES";
private static final String KEY_TIDIOMA = "notset";
private static final String KEY_PASSWORD = "pass";
private static final String KEY_PIRACY = "mykey";
private static final String KEY_URLFTP = "";
private static final String KEY_USERFTP = "";
private static final String KEY_PASSFTP = "";
private static final String KEY_PORTFTP = "";
// Email address (make variable public to access from outside)
public static final String KEY_UPDATE = "notasked";
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String n, String r, String col,String l){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing regcode in pref
editor.putString(KEY_NOMBRE, n);
// Storing regcode in pref
editor.putString(KEY_REGISTRO, r);
// Storing regcode in pref
editor.putString(KEY_COLEGIADO, col);
// Storing email in pref
editor.putString(KEY_IDIOMA, l);
// Set Piracy system
editor.putString(KEY_PIRACY, getMW());
// commit changes
editor.commit();
}
public void createFTPLoginSession(String url, String user, String password,String port){
System.out.println(url + " " + user + " " + password + " " + port);
// Storing login value as TRUE
editor.putBoolean(IS_LOGINFTP, true);
// Storing url ftp in pref
editor.putString(KEY_URLFTP, url);
// Storing userftp in pref
editor.putString(KEY_USERFTP, user);
// Storing ftp pass in pref
editor.putString(KEY_PASSFTP, password);
// Storing ftp port in pref
editor.putString(KEY_PORTFTP, port);
// commit changes
editor.commit();
}
public String getFTPUrl(){
return pref.getString(KEY_URLFTP, null);
}
public String getFTPUser(){
return pref.getString(KEY_USERFTP, null);
}
public String getFTPpass(){
return pref.getString(KEY_PASSFTP, null);
}
public String getFTPPort(){
return pref.getString(KEY_PORTFTP, null);
}
public String getPiracy(){
return pref.getString(KEY_PIRACY, null);
}
public String getMW(){
WifiManager wifiManager = (WifiManager) _context.getSystemService(_context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
return macAddress;
}
public String getLanguage(){
return pref.getString(KEY_IDIOMA, null);
}
public void setTlanguage(String l)
{
editor.putString(KEY_TIDIOMA, l);
editor.commit();
}
public String getTlanguage()
{
return pref.getString(KEY_TIDIOMA, "null");
}
/**
* 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);
}
}
public void checkFTPLogin(){
// Check login status
if(!this.isLoggedInFTP()){
// // 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_REGISTRO, pref.getString(KEY_REGISTRO, null));
// user email id
user.put(KEY_NOMBRE, pref.getString(KEY_NOMBRE, null));
// return user
return user;
}
/**
* Clear session details
* */
public void logoutFTPUser(){
// Storing login value as TRUE
editor.putBoolean(IS_LOGINFTP, false);
// Storing url ftp in pref
editor.putString(KEY_URLFTP, "");
// Storing userftp in pref
editor.putString(KEY_USERFTP, "");
// Storing ftp pass in pref
editor.putString(KEY_PASSFTP, "");
// Storing ftp port in pref
editor.putInt(KEY_PORTFTP, 0);
// commit changes
editor.commit();
}
/**
* Quick check for login
* **/
// Get Login State
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
// Get Login State
public boolean isLoggedInFTP(){
return pref.getBoolean(IS_LOGINFTP, false);
}
}
始终在所有KEYS中存储尝试存储的最后一个prefence,例如:如果最后一个参数是KEY_PORTFTP中的端口,则在尝试打印所有其他键时打印KEY_PORTFTP值。
一定是傻但我无法看到 有人可以帮助我吗?
答案 0 :(得分:0)
public void logoutFTPUser(){
// Storing login value as TRUE
editor.putBoolean(IS_LOGINFTP, false);
// Storing url ftp in pref
editor.putString(KEY_URLFTP, "");
// Storing userftp in pref
editor.putString(KEY_USERFTP, "");
// Storing ftp pass in pref
editor.putString(KEY_PASSFTP, "");
// Storing ftp port in pref
editor.putInt(KEY_PORTFTP, 0);
// commit changes
editor.commit();
}
Instead of this You can use like follow
public void logoutFTPUser(){
// Storing login value as TRUE
editor.putBoolean(IS_LOGINFTP, false);
editor.commit();
// Storing url ftp in pref
editor.putString(KEY_URLFTP, "");
editor.commit();
// Storing userftp in pref
editor.putString(KEY_USERFTP, "");
editor.commit();
// Storing ftp pass in pref
editor.putString(KEY_PASSFTP, "");
editor.commit();
// Storing ftp port in pref
editor.putInt(KEY_PORTFTP, 0);
// commit changes
editor.commit();
}
and Now You can get All The Items. Once You Assign the Value for One Key. You Should Commit the Editor. Then,Only you can get that value