2016-10-05 39 views

回答

0

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

SharedPreferences tutorial

希望它能帮助。干杯!

+1

感谢与其他articales组合我发现你的答案是非常有用的 –

+0

@DanielAnishchenko,欢迎:) –

0

我会推荐最好的方法。无论什么时候你被困在这样的事情上,启动屏幕将帮助你。你可以在启动画面上看到我正在检查我是否曾经登录过,并使用它转移到其他活动。

让我来解释如何: -

只是要启动屏幕启动活动。如果你不想让它显示更长的时间,只需让处理程序运行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(); 

    } 
} 

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

+0

我用这个不同的方法,但我用闪屏方法来作出加载屏幕,谢谢你帮助! –

相关问题