How to pause / sleep thread or process in Android?

后端 未结 12 1026
故里飘歌
故里飘歌 2020-11-22 05:49

I want to make a pause between two lines of code, Let me explain a bit:

-> the user clicks a button (a card in fact) and I show it by changing the background of thi

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:30

      class MyActivity{
        private final Handler handler = new Handler();
        private Runnable yourRunnable;
        protected void onCreate(@Nullable Bundle savedInstanceState) {
           // ....
           this.yourRunnable = new Runnable() {
                   @Override
                   public void run() {
                       //code
                   }
                };
    
            this.handler.postDelayed(this.yourRunnable, 2000);
           }
    
    
         @Override
      protected void onDestroy() {
          // to avoid memory leaks
          this.handler.removeCallbacks(this.yourRunnable);
          }
        }
    

    And to be double sure you can be combined it with the "static class" method as described in the tronman answer

提交回复
热议问题