如何保持用户登录的会议?

问题描述:

我有一个应用程序需要用户进行注册。我有conected到PHP应用程序在我的服务器做注册/登录的逻辑,因此,这并不是问题的了。

I have an app that requires user to register. I've got the app conected to PHP in my server to do the logic of register/login and so, this is not problem at all.

不过,我想保留在本地用户的会话,因此用户不必每次他运行应用程序时重新登录。

But, I want to keep a session of the user in local, so the user doesn't have to login again every time he runs the app.

所以,我想是这样的:

  • 在初次登录的用户,他打开应用程序,注册和登录。
  • 在做一些东西的应用程序,并关闭它。
  • 在再次打开的应用程序,用户被识别,所以他并不需要重新登录。

我只需要存储一个ID和一个用户名(从数据库中登录的PHP方法既牵强)。什么是做到这一点?最好的办法

I only need to store an ID and a Username (both fetched from the DB in the login php method). What's the best way to do this??

我想过做一个自定义的preference,存储在文件中的数据,甚至是本地数据库(源码)。有什么建议?

I've thought about doing a custom preference, store the data in files or even a local DB (sqlite). Any suggestions?

是啊,preferences会工作。在你的的onCreate()

Yeah, preferences would work. In your onCreate():

mPrefs = PreferenceManager.getDefaultSharedPrefs(this);

和一些功能,用户登录后会被调用的:

And in some function that gets called after the user logs in:

SharedPreferences.Editor edit = mPrefs.edit();
edit.putString("app_username", username);
edit.putInt("app_userid", userId);
edit.commit();