How to check if current user is logged in android

前端 未结 5 977
日久生厌
日久生厌 2020-12-14 23:24

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

相关标签:
5条回答
  • 2020-12-14 23:59

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

    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 me functionality by setting some cookie

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

    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)

    0 讨论(0)
  • 2020-12-15 00:03

    According to this:

    To check if the user is signed-in, call 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.
       // ...
    }
    
    0 讨论(0)
  • Save the state in PreferenceManager.

    You can see a little example here

    0 讨论(0)
  • 2020-12-15 00:07

    It can be done by sharedpreferences and 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 true and after that any time just fetch that Boolean and if true then {my info activity} other wise {register activity} simple is it dude :)

    like this for using sharedpreferences when first time login then

    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 
    }
    
    0 讨论(0)
  • 2020-12-15 00:08

    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 this example.

    0 讨论(0)
提交回复
热议问题