Execute function after 5 seconds in Android

后端 未结 9 1000
既然无缘
既然无缘 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:34

    Since, Handler is now deprecated so use this code :

     new Handler(Looper.myLooper()).postDelayed(new Runnable() {
         @Override
         public void run() {
             //do what you want 
         }
     }, 5000);
    
    0 讨论(0)
  • 2020-12-13 13:36

    Assign millisDelayTime variable with the milliseconds you desire to cause a delay. mActivity is an object of Activity for providing Application Context. In your case millisDelayTime should be initialized with 5000

    mActivity.runOnUiThread(new Runnable() {
    @Override
        public void run() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
                 //your code here
           }
        }, millisDelayTime);
      }
    });
    
    0 讨论(0)
  • 2020-12-13 13:45

    The best option to achieve this is using a Handler:

    int TIME = 5000; //5000 ms (5 Seconds)
    
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
    
            function(); //call function!
    
        }
    }, TIME);
    
    0 讨论(0)
提交回复
热议问题