如何保持用户登录的会议? [英] How to keep the session of user login?

查看:170
本文介绍了如何保持用户登录的会议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要用户进行注册。我有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();

这篇关于如何保持用户登录的会议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆