简体   繁体   中英

Shared preferences between classes (static)?

I have two classes, one loggedIn, and a User class. In the loggedIn class I want to show the shared preferences that I made when the user logs in.

loginPrefs = getSharedPreferences("loginpreferences",Context.MODE_PRIVATE);
SharedPreferences.Editor loginEditor = loginPrefs.edit();
loginEditor.putString("userID", userIDCrypt);
loginEditor.commit();

Now i want to make in the user class a getID() method, that I can call the method from every class with User.getID();. How can I do this?

I need the userID in multiple classes, so I want one activity (called getID eg) that gives me the user ID.

do like this make one class for your sharedpreference

       public class Session {

            private SharedPreferences prefs;

                public Session(Context cntx) {
                    // TODO Auto-generated constructor stub
                    prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
                }

           public void setusename(String usename) {
            prefs.edit().putString("usename", usename).commit();
            prefsCommit();
        }

        public String getusename() {
            String usename = prefs.getString("usename","");
            return usename;
        }
}

now after making this class when u want to use this use like this make object og this class like

  private Session session;//global variable 
session = new Session(cntx); //in oncreate 

and now we set sharedpreference then use this like

session.setusename("USERNAME");

now when ever u want to get username then same work for session object and call this

 session.getusename();

best of luck :) same for password

try this in one Activity :

SharedPreferences sp;
SharedPreferences.Editor edit;
sp = getSharedPreferences("enter", MODE_PRIVATE);
edit = sp.edit();
edit.putString("name", username);
edit.putString("pwd", password);
edit.commit();

in next activity :

SharedPreferences sp = getSharedPreferences("enter", MODE_PRIVATE);
sp.getString("name", "default value"));
sp.getString("pwd", "default value"));

Sorry if there is any syntax or compilation issues, just typing out of my head, and might make some mistakes. So in essence this is an approach that you can use to expose your prefs from one simple class and access it from any where.

public class MySharedPrefs {
    public SharedPreferences sp;

    public MySharedPrefs()
    {
        this.sp = c.getSharedPreferences("prefs", Context.MODE_PRIVATE);
    }

    public static String getStringFieldValue(Context c, String fieldName)
    {
        MySharedPrefs p = new MySharedPrefs(c);
        return p.sp.getString(fieldName, "default value");
    }

    public static void setStringValue(Context c, String fieldName, String value)
    {
        MySharedPrefs p = new MySharedPrefs(c);
        SharedPreferences.Editor edit;
        edit = p.sp.edit();
        edit.putString(fieldName, value);
        edit.commit();
    }

}

Then use it like this in your activity:

MySharedPrefs.getStringFieldValue(this, "name");

You can the also expand on this class and add additional helper methods such as a getUserName or etc.

UPDATE:

When calling this from another static class, that class needs to have a reference to your applications context, you then need to provide that context to this function instead of using this.

MySharedPrefs.getStringFieldValue(context, "name"); //if your other static class has a property called context that contains your applications context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM