Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delay some action in Android so that a task completes after a set Delay? [duplicate]

I am trying to develop an app in Android which has a few actions that need to be executed after a set period of time.

Anything which I can do to fulfil this?

like image 932
Utkarsh Avatar asked Nov 25 '25 15:11

Utkarsh


1 Answers

All the answer are correct but initiating handler like below is deprecated.

new Handler()

You need to provide a looper to the handler object. So use below snippet:

Handler(Looper.getMainLooper()).postDelayed({
   // do something
}, 1000)

Looper.getMainLooper() will ensure that code written in that block runs on UI thread.

More details in my other answer: How to handle deprecated Handler in android

like image 82
Alpha 1 Avatar answered Nov 27 '25 04:11

Alpha 1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!