Execute function after 5 seconds in Android

2019-01-23 04:02发布

I am new in android development and now my launcher activity show only 5 seconds and after that I want to check the user is logged in or not function and perform the actions.

here is my code.

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

    exactPreferences = getSharedPreferences("ExactPreference",MODE_PRIVATE);
    setContentView(R.layout.activity_landing_page);

    session = exactPreferences.getString(Model.getSingleton().SHARED_SESSION_ID,null);
    Log.i("Session Id",session);
        displayData(); // I want to perform this function after 5 seconds.
}


private void displayData() {
    if(session.equals("")){
        Intent loginIntent = new Intent(LandingPage.this,
                LoginActivity.class);
        startActivity(loginIntent);
        Log.i("User Logged In", "False");
    }
    else
    {
        Intent objIntent = new Intent(LandingPage.this,
                IndexPageActivity.class);
        startActivity(objIntent);
        Log.i("User Logged In", "True");
    }

}

7条回答
时光不老,我们不散
2楼-- · 2019-01-23 04:26

Try this, code create CountDownTimer with one tick

timer = new CountDownTimer(5000, 5000)
{
    public void onTick(long millisUntilFinished)
    {
    }

    public void onFinish()
    {
        displayData();
    }
};
timer.start();
查看更多
登录 后发表回答