在Android中5秒后执行功能 [英] Execute function after 5 seconds in Android

查看:1059
本文介绍了在Android中5秒后执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,现在我的启动器活动只显示5秒,之后我想检查用户是否已登录或未执行操作并执行操作。

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.

这是我的代码。

@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");
    }

}


推荐答案

您可以使用处理程序添加一些延迟。请按照以下方法调用方法 displayData(),以便在5秒后执行。

You can use the Handler to add some delay.Call the method displayData() as below so that it will be executed after 5 seconds.

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
          displayData();
        }
    }, 5000);

注意:不要使用像这样的线程Thread.sleep(5000); 因为它会阻止你的用户界面并使其无法响应。

Note : Do not use the threads like Thread.sleep(5000); because it will block your UI and and makes it irresponsive.

这篇关于在Android中5秒后执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆