就我而言,如何在 Android 中存储用户状态 [英] How to store user state in Android in my case

查看:33
本文介绍了就我而言,如何在 Android 中存储用户状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题是因为我想确定哪种方式是在我的 android 应用程序中存储用户登录"/注销"状态的最佳方式.

I'm asking this question because I want to be sure which is the best way to store the user "logged in" / "logged out" state in my android application.

这就是我正在做的事情.在我的应用程序中,我连接到服务器,向他发送用户名和密码,如果一切正常,它会返回我 OK.所以现在我希望能够在我的应用程序中保存用户登录状态,因为我需要更改内容取决于用户是否登录.

Here is what I am doing. In my application I connect to the server, send him username and password and if everything is ok it returns me OK. So now I want to be able to save the user logged in state all over my application because I need to change the content depends on that if user is logged in or not.

还有一点是我需要能够保存用户的登录状态,所以下次他启动应用程序时他会自动登录.

And the other thing is that I need to be able to save the user's login state, so the next time when he start the application he will be automatically logged in.

有什么建议或想法是最好的方法吗?

Any suggestions or ideas which is the best way to do that?

我在互联网上做了一些研究,并找到了一些方法:

I made a little research over internet and find a few ways to do that :

  1. 扩展 android.app.Application 的 MyApplication 类
  2. 共享偏好
  3. 或者也许是一个 SQLite

推荐答案

当您的应用失去焦点时可能有很多原因,例如按下主页按钮来电等,但无论如何您的 onPause当前 Activity 的方法被调用,所以你必须做的是-

There can be many reasons when your app looses focus, like pressing home button incoming call, etc but no matter what happens your onPause method of current Activity is called so what you have to is-

  1. 覆盖每个活动的 onPause 方法
  2. 使用SharedPreference保存应用程序的状态
  1. Override onPause method of every activity
  2. Use SharedPreference to save the state of Application

这篇关于就我而言,如何在 Android 中存储用户状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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