How to start a different activity with some delay after pressing a button in android?

后端 未结 7 684
感动是毒
感动是毒 2020-12-30 04:14

I want that a new activity should start with some delay on pressing a button. Is it possible to do that , and whats the procedure.

7条回答
  •  被撕碎了的回忆
    2020-12-30 04:43

    You could call a Runnable using the Handler postDelayed() method.

    Here's an example (http://developer.android.com/resources/articles/timed-ui-updates.html):

    private Handler mHandler = new Handler();
    
    ...
    
    OnClickListener mStartListener = new OnClickListener() {
       public void onClick(View v) {
                mHandler.postDelayed(mUpdateTimeTask, 100);
       }
    };
    
    private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           // do what you need to do here after the delay
       }
    };
    

    Props to @mad for getting it right the first time around.

提交回复
热议问题