How to display only one time Login and then after

2020-02-24 04:43发布

问题:

I am getting trouble in making only one time login... My aim is first user gets login screen.. If he is new user he will register and then login...from then when ever user starts the application he should directly redirect to main activity that is to skip the login page..please friends help me out from this problem..please post me any tutorials or any code...please tell me how to modify in manifest file also...

I am using like this in login activity but i didn't achieve my task.

SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
editor.putString("register","true");
editor.commit();
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true"))
   // redirect to next activity
else
   // show registration page again

回答1:

check it here

http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

A very good example of session management in android app.



回答2:

Implement your SharedPreferences this way:

Boolean isFirstTime;

SharedPreferences app_preferences = PreferenceManager
            .getDefaultSharedPreferences(Splash.this);

SharedPreferences.Editor editor = app_preferences.edit();

isFirstTime = app_preferences.getBoolean("isFirstTime", true);

if (isFirstTime) {

//implement your first time logic
editor.putBoolean("isFirstTime", false);
editor.commit();

}else{
//app open directly
}


回答3:

Use SharedPreferences. contains which tell an key is present or not in SharedPreferences. Change your code as:

   SharedPreferences pref;
   SharedPreferences.Editor editor;
   pref = getSharedPreferences("testapp", MODE_PRIVATE);
   editor = pref.edit();
   if(pref.contains("register"))
    {
         String getStatus=pref.getString("register", "nil");
          if(getStatus.equals("true")){
             redirect to next activity
           }else{
            //first time
             editor.putString("register","true");
             editor.commit();
           ///  show registration page again
           }
    }
    else{ //first time
             editor = pref.edit();
             editor.putString("register","true");
             editor.commit();
           ///  show registration page again
    }


回答4:

You can visit my blog

http://upadhyayjiteshandroid.blogspot.in/2013/01/android-working-with-shared-preferences.html

hope you will get the answer and understanding clearly

Boolean flag;

SharedPreferences applicationpreferences = PreferenceManager
            .getDefaultSharedPreferences(MainActivity.this);

SharedPreferences.Editor editor = applicationpreferences .edit();

flag = applicationpreferences .getBoolean("flag", false);

if (flag) {
///second time activity

}else{
//first time
editor.putBoolean("flag", true);
editor.commit();
}


回答5:

Check out the Session Management in Android which shows you the way how you can manage the login if user is already logged into the application or not. And switch the user accordingly.

Hope this will help you.



回答6:

1.for storing in stored preferences use this

 SharedPreferences.Editor editor = getSharedPreferences("DeviceToken",MODE_PRIVATE).edit();
                        editor.putString("DeviceTokenkey","ABABABABABABABB12345");
editor.apply();

2.for retrieving the same use

    SharedPreferences prefs = getSharedPreferences("DeviceToken", 
 MODE_PRIVATE);
    String deviceToken = prefs.getString("DeviceTokenkey", null);