如何检查当前用户是否已登录android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22526950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:07:58  来源:igfitidea点击:

How to check if current user is logged in android

android

提问by melwinpintoe

How to check if current user is logged in android. I have an external database, I have successfully connected it to the database. I need to check if the user is logged in or not

如何检查当前用户是否登录android。我有一个外部数据库,我已成功将其连接到数据库。我需要检查用户是否登录

if user not logged will display {register activity} otherwise will display {my info activity}

如果用户未登录将显示{注册活动}否则将显示{我的信息活动}

采纳答案by diegomercado

According to this:

根据这个

To check if the user is signed-in, call isConnected().

要检查用户是否已登录,请调用 isConnected()。

if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
   // signed in. Show the "sign out" button and explanation.
   // ...
} else {
   // not signed in. Show the "sign in" button and explanation.
   // ...
}

回答by Bhanu Sharma

It can be done by sharedpreferencesand if you want to do from database then take one Boolean and save log in info according to user id and then when you first time login then make it trueand after that any time just fetch that Boolean and if true then {my info activity} other wise {register activity} simple is it dude :)

它可以通过sharedpreferences如果你想从数据库中做然后取一个布尔值并根据用户 ID 保存登录信息,然后当你第一次登录时创建它true,之后任何时候只要获取那个布尔值,如果为真,那么 {我的信息活动}其他明智的{注册活动}很简单,伙计:)

like this for using sharedpreferenceswhen first time login then

像这样sharedpreferences在第一次登录时使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("Islogin", Islogin).commit(); // islogin is a boolean value of your login status

and anytime when u want to get status then

以及任何时候你想获得状态的时候

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        boolean Islogin = prefs.getBoolean("Islogin", false); // get value of last login status

Now check

现在检查

if(Islogin)
        {   // condition true means user is already login
            Intent i = new Intent(this, "your login activity");
            startActivityForResult(i, 1);
        }

else
{
    // condition false take it user on login form 
}

回答by MiguelAngel_LV

Save the state in PreferenceManager.

在 PreferenceManager 中保存状态。

You can see a little example here

你可以在这里看到一个小例子

回答by iTech

If you just want to test if the user is logged in to the device, it is sufficient to do this with SharedPreferenceas others indicated.

如果您只想测试用户是否登录到设备,SharedPreference按照其他人的指示进行操作就足够了。

However, if you want to ensure that the user is logged in to your external website, it is more complicated and requires more work.

但是,如果要确保用户登录到您的外部网站,则比较复杂,需要做更多工作。

1- Your website should have remember mefunctionality by setting some cookie

1-您的网站应该通过设置一些cookie来记住我的功能

2- Once the user logged in successfully to your website from the app; you need to preserve the remember mecookie (e.g. serialize it to some file)

2- 一旦用户从应用程序成功登录到您的网站;您需要保留记住我的cookie(例如将其序列化为某个文件)

3- When the user open the app, you have to load that cookie if exists and make a request to the server to ensure if the user still logged in or not (e.g. handle the HTTP response code)

3-当用户打开应用程序时,您必须加载该cookie(如果存在)并向服务器发出请求以确保用户是否仍然登录(例如处理HTTP响应代码)

回答by Aliti

This can be done in two ways. One is storing them in a global variables and second is storing the data in shared preferences. see thisexample.

这可以通过两种方式完成。一是将它们存储在全局变量中,二是将数据存储在共享首选项中。看到这个例子。