简体   繁体   English

类之间的共享首选项(静态)?

[英]Shared preferences between classes (static)?

I have two classes, one loggedIn, and a User class. 我有两个类,一个是LoginIn,另一个是User类。 In the loggedIn class I want to show the shared preferences that I made when the user logs in. 在loginIn类中,我想显示用户登录时所做的共享首选项。

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();. 现在我想在用户类中创建一个getID()方法,以便可以使用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. 我需要在多个类中使用userID,因此我需要一项活动(例如称为getID)来提供用户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 现在我们设置sharedpreference然后像这样使用

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. 您也可以在此类上进行扩展,并添加其他帮助方法,例如getUserName或类似方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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