如何只显示一次登录然后直接在android中启动应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21980324/
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 05:23:31  来源:igfitidea点击:

How to display only one time Login and then after start application directly in android

androidsharedpreferences

提问by user3304617

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

采纳答案by user3304617

check it here

在这里检查

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

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

A very good example of session management in android app.

android 应用程序中会话管理的一个很好的例子。

回答by M D

Implement your SharedPreferencesthis way:

SharedPreferences这种方式实施:

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
}

回答by ρяσ?ρ?я K

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

使用共享首选项。包含告诉 SharedPreferences 中是否存在键。将您的代码更改为:

   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
    }

回答by GrIsHu

Check out the Session Management in Androidwhich 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.

查看Android 中会话管理,它向您展示了在用户是否已登录到应用程序时如何管理登录的方式。并相应地切换用户。

Hope this will help you.

希望这会帮助你。

回答by Syed Danish Haider

1.for storing in stored preferences use this

1.为了存储在存储的首选项中使用这个

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

2.for retrieving the same use

2.用于检索相同的用途

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

回答by Jitesh Upadhyay

You can visit my blog

你可以访问我的博客

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

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();
}