Keep the account info in all activities in android studio

前端 未结 2 1585
庸人自扰
庸人自扰 2021-01-24 09:20

My application is simply a managing database application. I have a remote mysql server set up and my android studio application uses http post request to connect to that server.

2条回答
  •  醉梦人生
    2021-01-24 09:43

    you could it done in many ways like store data in sharedpreference or database or simply create a class that contains static variables.

    To store and get data from shared preference use this.

    public class MyPreference {
    
    SharedPreferences myPrefs;
    SharedPreferences.Editor prefEditor;
    Context context;
    
    // ------------------------------------------------------------------costructor
    public MyPreference(Context context) {
        this.context = context;
        myPrefs = context.getSharedPreferences("myPrefs", 0);
    }
    
    // ----------------------------------------------------------------------- user login
    
    public void setLogin() {
    
        prefEditor = myPrefs.edit();
        prefEditor.putBoolean("login", true);
        prefEditor.commit();
    
    }
    
    public boolean isLogin() {
        return myPrefs.getBoolean("login", false);
    }
    
    public void setPassword(String userPass) {
        prefEditor = myPrefs.edit();
        prefEditor.putString("user", userPass);
        prefEditor.commit();
    }
    
    public String getPassword() {
        return myPrefs.getString("user", "");
    }
    }
    

提交回复
热议问题