如何在Android中保持会话?如何在、Android

2023-09-06 16:11:39 作者:舔奶盖的小仙女

谁能告诉我如何保持会话的用户登录。例如当他们有一个应用程序的用户先签后换将签署的,除非用户注销或卸载类似的应用程序在Android中到Gmail。

Can anybody tell me how to maintain session for a user login. For example when the user sign- in to an application they have to be signed in unless the user logouts or uninstall the application similar to gmail in android.

推荐答案

就这样让一个类的共享preference

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;
    }
}

现在做这个类当U想用这种使用这样的化妆对象OG这个类像

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();

好运:)相同的密码