简体   繁体   中英

Android: Changing activity start my app depending on whether the user is logged in or not?

我必须像Spotify这样开始,如果用户未登录,将显示一个屏幕,如果用户登录,则显示另一个屏幕,任何人都可以提供帮助,谢谢。

Try this code, It may helpful for you.

public class SplashActivity extends Activity {
boolean isUserLoggedIn = true;
// User Session Manager Class
UserSessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    session = new UserSessionManager(getApplicationContext());
    isUserLoggedIn = session.isUserLoggedIn();
    if(isUserLoggedIn)
    {
        Intent intent = new Intent(SplashActivity.this,FirstActivity.class);
        startActivity(intent);
        SplashActivity.this.finish();
    }
    else
    {
    setContentView(R.layout.splash_layout);
    Thread timer = new Thread(){
        public void run() {
            try
            {
                sleep(3000);
            }catch(InterruptedException e) { }
            finally
            {
                Intent intent = new Intent(SplashActivity.this,SecondActivity.class);
                startActivity(intent);
                SplashActivity.this.finish();
            }
        }
    };
    timer.start();
    }
}}

UserSessionManager Class :-

public class UserSessionManager
{
public static final String KEY_EMAIL = "email";
public static final String KEY_NAME = "name";
int PRIVATE_MODE = 0;
Context _context;
SharedPreferences.Editor editor;
SharedPreferences pref;

public UserSessionManager(Context paramContext)
{
    this._context = paramContext;
    this.pref = this._context.getSharedPreferences("AndroidPref", this.PRIVATE_MODE);
    this.editor = this.pref.edit();
}

public void createUserLoginSession(String paramString1, String paramString2)
{
    this.editor.putBoolean("IsUserLoggedIn", true);
    this.editor.putString("name", paramString1);
    this.editor.putString("email", paramString2);
    this.editor.commit();
}

public HashMap<String, String> getUserDetails()
{
    HashMap<String, String> localHashMap = new HashMap<String, String>();
    localHashMap.put("name", this.pref.getString("name", null));
    localHashMap.put("email", this.pref.getString("email", null));
    return localHashMap;
}

public boolean isUserLoggedIn()
{
    return this.pref.getBoolean("IsUserLoggedIn", false);
}

public void logoutUser()
{

}}

You can set login detail using createUserLoginSession method.

If You don't want to use splash waiting. simply remove setContentView method and Thread in SplashActivity class.

One option is to use a "splash" activity that evaluates some persisted state (like an oauth token) and depending on that state, either start your login activity or go ahead into your root logged-in activity.

If this check is fast and you do everything inside of onCreate(), you won't even see this splash activity. If the check takes a bit, you can show a splash view with a logo or whatever until it's done.

Don't forget to finish() the splash activity once you leave it (or start the new one with the clear task flags). Otherwise, when your user presses back, they will hit the splash activity instead of closing out of your application as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM