【问题标题】:Could auto-change the public static String parameter?可以自动更改公共静态字符串参数吗?
【发布时间】:2012-11-02 05:12:39
【问题描述】:

类包含这个参数就像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”。

为什么???

有人知道吗?

【问题讨论】:

  • 使用共享偏好来保存密码。

标签: android static-members


【解决方案1】:

我认为您的类已通过类加载器(在 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();

现在您可以使用变量,即 USER_ID 使用 global.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");
          }
        }

【讨论】:

    【解决方案2】:
    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();
       }
    
    }
    

    【讨论】:

    • 使用这个类来保存密码、恢复、删除凭据。
    【解决方案3】:
    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");
    

    并从同一个 Like 中获取值

    String USER_ID = Globals.userDetails.getUSER_ID();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多