Execute function after 5 seconds in Android

后端 未结 9 999
既然无缘
既然无缘 2020-12-13 12:39

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

相关标签:
9条回答
  • 2020-12-13 13:20
    long delay = 1000;
    long period = 50000;
    Timer task = new Timer();
    task.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            getDriver(sessionManager.getKEY(), ride_id);
        }
    }, delay, period);
    
    0 讨论(0)
  • 2020-12-13 13:23

    For kotlin way

    Handler().postDelayed({
            //do something
        }, 5000)
    
    0 讨论(0)
  • 2020-12-13 13:28

    Try this, code create CountDownTimer with one tick

    timer = new CountDownTimer(5000, 5000)
    {
        public void onTick(long millisUntilFinished)
        {
        }
    
        public void onFinish()
        {
            displayData();
        }
    };
    timer.start();
    
    0 讨论(0)
  • 2020-12-13 13:28

    When possible, try to avoid using postDelayed. It is a bad practice, since it can lose the reference to the objects that you want to draw on your screen and cause a NPE. Use a Handler instead. First of all, create a global variable Handler in which you will have to "handle" the logic for your code. Do so by using the function handleMessage.

    Handler  handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                // your code here
            }
        }
    };       
    

    Then, wherever you want to execute it, just call the function:

    // 1 is the ID of your process
    handler.sendEmptyMessageDelayed(1, 5000);
    

    Please remember that in the onDestroyView method (in a Fragment) or the onDestroy (in an Activity) you will have to call

        handler.removeMessages(1)
    
    0 讨论(0)
  • 2020-12-13 13:31

    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);
    

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

    0 讨论(0)
  • 2020-12-13 13:31

    Use a CountDownTimer

    // There's a TextView txtCount in Main Activity
    
    final int secs = 5;
    new CountDownTimer((secs +1) * 1000, 1000) // Wait 5 secs, tick every 1 sec
    {
        @Override
        public final void onTick(final long millisUntilFinished)
        {
            txtCount.setText("" + (int) (millisUntilFinished * .001f));
        }
        @Override
        public final void onFinish()
        {
            txtCount.setText("GO!");
            finish();
            // Time's up - Start the Login Activity
            final Intent tnt =
                new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(tnt);
        }
    }.start();
    
    0 讨论(0)
提交回复
热议问题