我试图为应用程序制作一个acsess代码

时间:2016-10-05 06:28:54

标签: android

我正在尝试为Android应用创建一个访问屏幕,其中用户提交了一次访问代码,然后将其保存到文件中,稍后启动屏幕应该被跳过,它应该直接进入以后的布局

我不知道如何做到这一切。

2 个答案:

答案 0 :(得分:0)

它被称为SharedPreferences。将您的访问代码保存在SharedPreference中,并检查您是否有任何访问代码。在那之后,你不会有任何问题。

SharedPreferences tutorial

希望它有所帮助。干杯!

答案 1 :(得分:0)

我会推荐最好的方法。每当你遇到这些事情时,Splash屏幕都会对你有所帮助。您可以在启动画面上看到我正在检查我之前是否登录过,并使用该活动转移到其他活动。

让我解释一下: -

只需将启动画面设为启动器活动即可。如果您不想让它显示更长时间,只需让处理程序运行1-2秒。现在看下面的代码。

  

SplashScreen.java

public class SplashScreen extends AppCompatActivity {

    private String email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_splash);

        //SharedPreference to Store API Result
        SharedPreferences pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.apply();

        email = pref.getString("login", null);

        int SPLASH_TIME_OUT = 3000;

        if (email != null) {

            //It means User is already Logged in so I will take the user to Select_College Screen

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                    Intent intent = new Intent(SplashScreen.this, Select_College.class);
                    intent.putExtra("Email", email);
                    startActivity(intent);
                    finish();

                }

            }, SPLASH_TIME_OUT);

        } else {

            //It means User is not Logged in so I will take the user to Login Screen

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                    Intent intent = new Intent(SplashScreen.this, Login.class);
                    startActivity(intent);
                    finish();

                }

            }, SPLASH_TIME_OUT);

        }

    }
}
  

Login.java

public class Login extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_login);

        //SharedPreference to Store API Result
        pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);

        Login();

    }

    private void login() {

        //If login is successfull, before moving to next activity, store something in sharedpreference with name login. It can be email or just a string as "true"

        SharedPreferences.Editor editor = pref.edit();
                        editor.putString("login", email);
                        editor.apply();

                        Intent intent = new Intent(DoctorLogin.this, Select_Collage.class);
                        intent.putExtra("Email", email);
                        startActivity(intent);
                        finish();

    }
}
  

Select_Collage.java

public class Select_Collage extends AppCompatActivity {

    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_message);

        //SharedPreference to Store API Result
        pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);

        //Somewhere on Signout button click, delete the login sharedpreference
        signOut();

    }

    private void signOut() {

        SharedPreferences.Editor editor = pref.edit();
        editor.remove("login");
        editor.apply();

        Intent intent = new Intent(Select_Collage.this, Login.class);
        startActivity(intent);
        finish();

    }
}

这就是你如何解决这个问题。保持编码:)