该类包含此参数,如thie
public class Global {
public static String USER_ID = "";
public static String USER_NAME = "";
public static String USER_PASSWORD = "";
}
USER_ID
,我在登录时设置了一次。(例如Global.USER_ID =“12345”)。
并且在后台运行的一个线程会每隔5分钟将一些数据(包含此USER_ID
)发布到web服务。
USER_ID
返回“”,不是“12345”。
为什么???
谁知道呢?答案 0 :(得分:2)
我认为你的类是通过类加载器(在GC期间)从内存中删除的,并且在下次使用Global类时会加载它。
我的建议是使用Singelton类,你可以有这样的东西
public class Global {
public String USER_ID = "";
public String USER_NAME = "";
public String USER_PASSWORD = "";
//constructor
private Global(){
// just to prevent object creation from outside
}
//static reference
public static Global global=null;
public Global getInstance(){
return ((global!=null)? global : (global=new Global()) ;
}
}
现在,无论你想使用这个类,你都可以得到这个对象,singelton逻辑确保它们只是一个在所有对象之间共享的对象。
您可以使用
在线程中获取引用global = Global.getInstance();
现在您可以使用 global.USER_ID
使用变量,例如 USER_ID由于你有一个全局对象的引用,因此在线程处于活动状态之前,GC不会从内存中删除它。
注意强> 我仅使用公共变量进行说明,但在大多数情况下不推荐使用它们。
修改强>
为了保存用户数据,我建议 SharedPreferences ,您可以按如下方式使用它们: -
private final String YourAppName_PREFS_NAME = "chooseAMeaningFulName";
private final String YourAppName_USERNAME = "username";
private final String YourAppName_PASSWORD = "password";
/*This function will save the user name and password for providing Automatically login in future*/
public void saveUserData() {
if ( rememberMeChckbox.isChecked() ) {
// Log.i("loginFrag", "Saving userName: Pass " + userName + " : " + password);
loginActivity.getSharedPreferences(YourAppName_PREFS_NAME, Context.MODE_PRIVATE).edit().putString(YourAppName_USERNAME, userName)
.putString(YourAppName_PASSWORD, password).commit();
} else {
loginActivity.getSharedPreferences(YourAppName_PREFS_NAME, Context.MODE_PRIVATE).edit().remove(RBR_USERNAME).remove(RBR_PASSWORD).commit();
}
}
/*This function returns the previous data from the SharedPreferences and fills the UserName and Password Text Box*/
private void getPreviousUserData() {
SharedPreferences preferences = loginActivity.getSharedPreferences(YourAppName_NAME, Context.MODE_PRIVATE);
userName = preferences.getString(YourAppName_USERNAME, null);
password = preferences.getString(YourAppName_PASSWORD, null);
// Log.i("loginFrag", "Got userName: Pass " + userName + " : " + password);
if ( userName != null ) {
uText.setText(userName);
} else {
uText.setText("android");
}
if ( password != null ) {
passText.setText(password);
} else {
passText.setText("android");
}
}
答案 1 :(得分:1)
public class Credentials {
private Context context;
private final String USERID = "USER_ID";
private final String USER_NAME = "USER_NAME";
private final String PASSWORD = "Password";
private final String KEY="SaveCredentials";
public Credentials(Context mcontext) {
super();
this.context = mcontext;
}
public boolean saveCredentials(String userId,String password,String UserName) {
Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(USERID,userId);
editor.putString(USER_NAME,UserName);
editor.putString(PASSWORD,password);
return editor.commit();
}
public ArrayList<String> restoreCredentials() {
ArrayList<String> credentials=new ArrayList<String>();
SharedPreferences sharedPreferences = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);
String userid= sharedPreferences.getString(USERID, null);
String userName=sharedPreferences.getString(USER_NAME,null);
String password=sharedPreferences.getString(PASSWORD,null);
credentials.add(userid);
credentials.add(userName);
credentials.add(password);
return credentials;
}
public void deleteCredentials()
{
Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
}
}
答案 2 :(得分:0)
public class UserDetail {
public String USER_ID = "";
public String USER_NAME = "";
public String USER_PASSWORD = "";
public static void setUSER_ID(String USER_ID)
{
this.USER_ID = USER_ID;
}
public static String getUSER_ID()
{
return USER_ID;
}
}
在Globals Class
声明
public static UserDetail userDetails = new UserDetail();
在您的代码中,首先设置值
Globals.userDetals.setUSER_ID("12345");
并从相同的
获取值String USER_ID = Globals.userDetails.getUSER_ID();