2016-10-05 72 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

我用這個不同的方法,但我用閃屏方法來作出加載屏幕,謝謝你幫助! –

相關問題